SqlDataAdapter sda = new SqlDataAdapter(); SqlConnection con = new SqlConnection();
con.ConnectionString = " Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=sa";
con.Open();
//找到最大员工id
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT MAX(customerid) FROM Customers WHERE len(customerid) <> 5";
cmd.Connection = con;
string sMaxno = cmd.ExecuteScalar().ToString();
//员工id号增加1
int i_tem = int.Parse(sMaxno) + 1;
sMaxno = i_tem.ToString();
SqlCommand cmdAdapter = new SqlCommand();
cmdAdapter.CommandText = "select CustomerID,CompanyName from customers";
cmdAdapter.Connection = con; SqlCommand inData = new SqlCommand();
inData.CommandText = "INSERT Customers(CustomerID,CompanyName)VALUES(@sCstmid,@scmpName)";
inData.Connection = con; inData.Parameters.Add("@sCstmid", SqlDbType.NVarChar, 5, "CustomerID");
inData.Parameters.Add("@scmpName", SqlDbType.NVarChar, 40, "CompanyName");
sda.SelectCommand = cmdAdapter;
sda.InsertCommand = inData; DataSet ds = new DataSet(); sda.Fill(ds, "Customers"); //提示该行错误 DataRow nRow = ds.Tables["Customers"].NewRow();
nRow["customerid"] = sMaxno.ToString();
nRow["CompanyName"] = sMaxno.ToString(); sda.Update(ds, "Customers");请高手指教
con.ConnectionString = " Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=sa";
con.Open();
//找到最大员工id
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT MAX(customerid) FROM Customers WHERE len(customerid) <> 5";
cmd.Connection = con;
string sMaxno = cmd.ExecuteScalar().ToString();
//员工id号增加1
int i_tem = int.Parse(sMaxno) + 1;
sMaxno = i_tem.ToString();
SqlCommand cmdAdapter = new SqlCommand();
cmdAdapter.CommandText = "select CustomerID,CompanyName from customers";
cmdAdapter.Connection = con; SqlCommand inData = new SqlCommand();
inData.CommandText = "INSERT Customers(CustomerID,CompanyName)VALUES(@sCstmid,@scmpName)";
inData.Connection = con; inData.Parameters.Add("@sCstmid", SqlDbType.NVarChar, 5, "CustomerID");
inData.Parameters.Add("@scmpName", SqlDbType.NVarChar, 40, "CompanyName");
sda.SelectCommand = cmdAdapter;
sda.InsertCommand = inData; DataSet ds = new DataSet(); sda.Fill(ds, "Customers"); //提示该行错误 DataRow nRow = ds.Tables["Customers"].NewRow();
nRow["customerid"] = sMaxno.ToString();
nRow["CompanyName"] = sMaxno.ToString(); sda.Update(ds, "Customers");请高手指教
的CompanyName和from之間是個中文的空白符號吧?
把這個空白換一下試試
nRow["customerid"] = sMaxno.ToString();
nRow["CompanyName"] = sMaxno.ToString();
id弄个字增长的不行?