ADO.NET中非批量操作
时间:2010-12-11 来源:hfliyi
为此,我特意把非批量操作的跟新,查找,添加,删除给予列出:
//数据的查询

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
SqlCommand cmd =new SqlCommand("SELECT * FROM CUSTOMERS$",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"Customers$");
this.datagridview.DataSource =ds.Tables["Customers$"];
//数据的插入

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT CustomerID,CompanyName,ContactName,ContactTitle FROM CUSTOMERS$", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Customers$");
DataRow dr = ds.Tables["Customers$"].NewRow();
dr["CustomerID"] = this.txtCustomerID.Text.Trim();
dr["CompanyName"] = this.txtCompanyName.Text.Trim();
dr["ContactName"] = this.txtContactName.Text.Trim();
dr["ContactTitle"] = this.txtContactTitle.Text.Trim();
ds.Tables["Customers$"].Rows.Add(dr);
da.InsertCommand = new SqlCommand("insert into Customers$(CustomerID,CompanyName,ContactName,ContactTitle) values('"+this.txtCustomerID.Text.Trim()+"','"+this.txtCompanyName.Text.Trim()+"','"+this.txtContactName.Text.Trim()+"','"+this.txtContactTitle.Text.Trim()+"')", con);
da.Update(ds.Tables["Customers$"]);
this.dataGridView1.DataSource = ds.Tables["Customers$"];
ds.Tables["Customers$"].NewRow();的意思是利用当前的表的架构来实例化一个新行的对象
//修改

string str = this.txtCompanyName.Text;
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT CustomerID,CompanyName,ContactName,ContactTitle FROM CUSTOMERS$", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Customers$");
int i = 0;
for (i = 0; i < ds.Tables["Customers$"].Rows.Count; i++)
{
if (ds.Tables["Customers$"].Rows[i][0].ToString() == this.txtCustomerID.Text.Trim())
{ break; }
}
ds.Tables["Customers$"].Rows[i][1] = this.txtCompanyName.Text;
ds.Tables["Customers$"].Rows[i][2] = this.txtContactName.Text.Trim();
ds.Tables["Customers$"].Rows[i][3] = this.txtContactTitle.Text.Trim();
da.UpdateCommand = new SqlCommand("update [Customers$] set CompanyName='"+this.txtCompanyName.Text.Trim()+"',ContactName='"+this.txtContactName.Text.Trim()+"',ContactTitle='"+this.txtContactTitle.Text.Trim()+"' where CustomerID='"+this.txtCustomerID.Text.Trim()+"'",con);
da.Update(ds.Tables["Customers$"]);
this.dataGridView1.DataSource = ds.Tables["Customers$"];
//删除

string str = this.txtCompanyName.Text;
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT CustomerID,CompanyName,ContactName,ContactTitle FROM CUSTOMERS$", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Customers$");
int i = 0;
for (i = 0; i < ds.Tables["Customers$"].Rows.Count; i++)
{
if (ds.Tables["Customers$"].Rows[i][0].ToString() == this.txtCustomerID.Text.Trim())
{ break; }
}
ds.Tables["Customers$"].Rows[i].Delete();
da.DeleteCommand=new SqlCommand("Delete From Customers$ where CustomerID ='"+this.txtCustomerID.Text.Trim()+"'",con);
da.Update(ds.Tables["Customers$"];
相关阅读 更多 +
排行榜 更多 +