文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ADO.NET中非批量操作

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$"];

 

 

 

相关阅读 更多 +
排行榜 更多 +
吹风机射击 v1.0 安卓版

吹风机射击 v1.0 安卓版

飞行射击 下载
吹风机射击 v1.0 安卓版

吹风机射击 v1.0 安卓版

飞行射击 下载
吹风机射击 v1.0 安卓版

吹风机射击 v1.0 安卓版

飞行射击 下载