类似的我写了一个向Sql Server 2000 的northwind数据库的customers表进行插入的程序,却成功,不解二者区别public string InsertDataSet(string dbConnectString,DataSet ds)
{
string sql = @"Insert into Customers (CustomerID,CompanyName,ContactName,ContactTitle)
Values ( @CustomerID,@CompanyName,@ContactName,@ContactTitle)"; SqlConnection dataConn = null;
SqlDataAdapter dsAdapter = null;
try
{
dataConn = new SqlConnection(dbConnectString);
dsAdapter = new SqlDataAdapter();
dsAdapter.InsertCommand = new SqlCommand(sql,dataConn);
dsAdapter.InsertCommand.Parameters.Add(new SqlParameter("@CustomerID",SqlDbType.NVarChar,40));
dsAdapter.InsertCommand.Parameters[0].SourceColumn = "CustomerID"; dsAdapter.InsertCommand.Parameters.Add(new SqlParameter("@CompanyName",SqlDbType.NVarChar,40));
dsAdapter.InsertCommand.Parameters[1].SourceColumn = "CompanyName";
dsAdapter.InsertCommand.Parameters.Add(new SqlParameter("@ContactName",SqlDbType.NVarChar,40));
dsAdapter.InsertCommand.Parameters[2].SourceColumn = "ContactName";
dsAdapter.InsertCommand.Parameters.Add(new SqlParameter("@ContactTitle",SqlDbType.NVarChar,40));
dsAdapter.InsertCommand.Parameters[3].SourceColumn = "ContactTitle"; dataConn.Open();
dsAdapter.Update(ds,"Customers");
}
catch (Exception exp)
{
return(exp.ToString());
}
finally
{
dataConn.Close();
}
return "";
}
{
string sql = @"Insert into Customers (CustomerID,CompanyName,ContactName,ContactTitle)
Values ( @CustomerID,@CompanyName,@ContactName,@ContactTitle)"; SqlConnection dataConn = null;
SqlDataAdapter dsAdapter = null;
try
{
dataConn = new SqlConnection(dbConnectString);
dsAdapter = new SqlDataAdapter();
dsAdapter.InsertCommand = new SqlCommand(sql,dataConn);
dsAdapter.InsertCommand.Parameters.Add(new SqlParameter("@CustomerID",SqlDbType.NVarChar,40));
dsAdapter.InsertCommand.Parameters[0].SourceColumn = "CustomerID"; dsAdapter.InsertCommand.Parameters.Add(new SqlParameter("@CompanyName",SqlDbType.NVarChar,40));
dsAdapter.InsertCommand.Parameters[1].SourceColumn = "CompanyName";
dsAdapter.InsertCommand.Parameters.Add(new SqlParameter("@ContactName",SqlDbType.NVarChar,40));
dsAdapter.InsertCommand.Parameters[2].SourceColumn = "ContactName";
dsAdapter.InsertCommand.Parameters.Add(new SqlParameter("@ContactTitle",SqlDbType.NVarChar,40));
dsAdapter.InsertCommand.Parameters[3].SourceColumn = "ContactTitle"; dataConn.Open();
dsAdapter.Update(ds,"Customers");
}
catch (Exception exp)
{
return(exp.ToString());
}
finally
{
dataConn.Close();
}
return "";
}
说明是存贮过程,但是你并没有用存贮过程呀!
改为:dsAdapter.DeleteCommand.CommandType = CommandType.Text;
首先取得数据,放到DataGrid里System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"student");然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"student");
如果需要,可以绑定TextBox来作录入,而用DataGrid显示
this.textBox16.DataBindings.Add("Text",dt,"student.stuno");
然后进行数据的操作如:
增加:
this.BindingContext[dt,"student"].AddNew();
删除:
this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);
最后把结果写回数据库:
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)";
this.sqlInsertCommand1.Connection = this.conn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
this.sqlUpdateCommand1.Connection = this.conn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
this.sqlDeleteCommand1.Connection = this.conn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDa.InsertCommand = this.sqlInsertCommand1;
this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
try
{
sqlDa.Update(dt.GetChanges,"student");
return true;
}
catch(System.Data.SqlClient.SqlException ex)
{
return false;
}
finally
{
conn.Close();
}