110,570
社区成员
发帖
与我相关
我的任务
分享
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection c1;
SqlDataAdapter da;
DataTable orders;
private void button1_Click(object sender, EventArgs e)
{
da.Update(orders);
c1.Close();
}
private void button3_Click(object sender, EventArgs e)
{
c1 = new SqlConnection("server=.;database=db;uid=sa;pwd=");
c1.Open();
da = new SqlDataAdapter("select * from tb_ltd_info", c1);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
orders = new DataTable();
da.Fill(orders);
this.textBox1.DataBindings.Add("text", orders, "Ltd_Name", false, DataSourceUpdateMode.OnPropertyChanged);
this.textBox2.DataBindings.Add("text", orders, "Ltd_Add", false, DataSourceUpdateMode.OnPropertyChanged);
this.textBox3.DataBindings.Add("text", orders, "Ltd_Phone", false, DataSourceUpdateMode.OnPropertyChanged);
this.textBox4.DataBindings.Add("text", orders, "Ltd_Fax", false, DataSourceUpdateMode.OnPropertyChanged);
this.textBox5.DataBindings.Add("text", orders, "Ltd_EMail", false, DataSourceUpdateMode.OnPropertyChanged);
}
}
}
orders.ColumnChanged += new DataColumnChangeEventHandler(Column_Changed);
private static void Column_Changed(object sender, DataColumnChangeEventArgs e)
{
e.Row.EndEdit();
}
public static SqlDataAdapter CreateCustomerAdapter(
SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter();
// Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection);
// Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15);
adapter.SelectCommand = command;
// Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);
// Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
adapter.InsertCommand = command;
//要加上这个UpdateCommand
// Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);
// Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.UpdateCommand = command;
// Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand = command;
return adapter;
}
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(orders);