从OdbcConnection拷贝数据到sqlstatement
本文关键字:sqlstatement 数据 拷贝 OdbcConnection | 更新日期: 2023-09-27 17:54:54
我想通过c#代码从OdbcConnection复制我的数据到sqlstatement。通过一个按钮,我想这样做……我对c#不是很精通,但只是一点编程,谁能在我们的语句/帮助中添加/编辑一些代码,这样我就可以完成我的程序了。
我通过这段代码,但数据不读取....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Data;
using Microsoft.VisualBasic;
using System.Runtime.CompilerServices;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
private OdbcConnection cn;
private SqlCommand DB_Cmd;
private SqlConnection DB_cn;
public const string DbConStr = "Data Source=172.16.4.8;Initial Catalog=anDb;User ID=sa; Password= ;";
private OdbcCommand Jda_Cmd;
//private long Jda_Order_no;
public const string JdaConStr = "DSN=Imports_Img;UserID=user2;Password=pass2;Data Source=172.16.4.5;Transport Product=Client Access;SSL=DEFAULT;DEFAULT COLLECTION='BINDAWOOD'";
//private string sku;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.cn = new OdbcConnection();
this.Jda_Cmd = new OdbcCommand();
this.Jda_Cmd.Connection = this.cn;
this.cn.ConnectionString = "DSN=Imports_Img;UserID=JUNAID;Password=PAKIKARA19;Data Source=172.16.4.5;Transport Product=Client Access;SSL=DEFAULT;DEFAULT COLLECTION='BINDAWOOD'";
string str = ("SELECT ibhand FROM MM410LIB.INVBAL WHERE INUMBR = 79610 AND ISTORE=14");
this.Jda_Cmd.CommandText = str;
{
this.cn.Open();
OdbcDataReader reader2 = this.Jda_Cmd.ExecuteReader();
//connection2.Open();
while (reader2.Read())
{
this.DB_Cmd = new SqlCommand();
this.DB_Cmd.Connection = this.DB_cn;
SqlConnection connection2 = new SqlConnection();
//SqlCommand db_cn = new SqlCommand ("insert into invbaldb ('sku') values ('" + reader2[0] + "')", connection2);
//SqlCommand db_cn = new SqlCommand("insert into invbaldb (sku) values ('" + reader2[0].ToString() + "')", connection2);
SqlCommand DB_cn = new SqlCommand("insert into invbaldb (sku) values (@sku)", connection2);
DB_cn.Parameters.AddWithValue("@sku", reader2[0].ToString());
//SqlCommand command2 = new SqlCommand("insert into invbaldb ('sku') values ('" + reader2[0] + "')", connection2);
//DB_cn.ExecuteNonQuery();
DB_cn.ExecuteNonQuery();
}
reader2.Close();
//connection2.Close();
}
}
}
如果表invbaldb
中的目标字段命名为sku
则不应该在名称周围加上单引号因为那样会被解释为字符串
SqlCommand db_cn = new SqlCommand ("insert into invbaldb (sku) values ('" +
reader2[0].ToString() + "')", connection2);
还应考虑使用参数化查询而不是字符串连接。
SqlCommand db_cn = new SqlCommand ("insert into invbaldb (sku) values (@sku)", connection2);
db_cn.Parameters.AddWithValue("@sku", reader[0].ToString());
EDIT如果目标字段是数字,则应该相应地键入参数
db_cn.Parameters.AddWithValue("@sku", Convert.ToInt32(reader[0].ToString()));
当然这意味着输入值是可以被转换成整数值的