我想通过C#代码将我的数据从OdbcConnection复制到SqlStatment。通过一个按钮,我想这样做...... 我对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();
}
}
}
答案 0 :(得分:0)
如果表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());
编辑如果目标字段是数字,则应相应地键入参数
db_cn.Parameters.AddWithValue("@sku", Convert.ToInt32(reader[0].ToString()));
当然这意味着输入值可以转换为整数值