文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ADO.NET对象模型 添加,删除,更新

ADO.NET对象模型 添加,删除,更新

时间:2010-11-02  来源:大 蜗 牛

 

 

 

使用Command对象和DataAdapter对象

SqlConnection conn=newSqlConnection(“server=(local);Initial Catalog=BookShop;Integrated Security=true”);

String sqr=”select * from Books”;

SqlCommand comm=new SqlCommand(sqr,conn);

try

{

Conn.Open();

SqlDataReader sdr=comm..ExecuteReader();

While(sdr.Read())

{

Console.WriteLine(sdr.GetString(1)+”,”+sdr.GetString(2));

}

Sdr.Close();

}

Catch(SqlException ex)

{

Console.WriteLine(ex.Message);

}

Finally

{

conn.Close();

}

 

 

更新数据

第一种是使用DataSet对象,第二种是使用SqlCommand对象

SqlConnection conn=new SqlConnection(“server=(local);Initial Catalog=BookShop;Integrated Security=true”);

SqlDataAdapter sda=new SqlDataAdapter(“select * from Publisher”,conn);

DataSet ds=new DataSet();

Sda.Fill(ds,”Publisher”);

SqlCommandBuilder scb=new SqlCommandBuilder(sda);

DataTable dt=new DataTable[“publisher”];

Dt.Rows[0][1]=”dsad”;

Sda.Update(ds,”Publisher”);

 

 

添加数据

SqlConnection conn=new SqlConnection(“server=(local);Initial Catalog=BookShop;Integrated Security=true”);

SqlDataAdapter sda=new SqlDataAdapter(“select * from Book”,conn);

DataSet ds=new DataSet();

Sda.Fill(ds,”Publisher”);

SqlCommandBuilder scb=new SqlCommandBuilder(sda);

DataRow row=ds.Tables[“Publisher”].NewRow();

Row[“Name”]=”我的出版社”;

ds.Tables[“Publisher”].Rows.Add(row);

sda.Update(ds,”Publisher”);

 

 

删除数据

SqlConnection conn=new SqlConnection(“server=(local);Initial Catalog=BookShop;Integrated Security=true”);

SqlDataAdapter sda=new SqlAdapter(“select * from Book”,conn);

DataSet ds=new DataSet();

Sda.Fill(ds,”Publisher”);

SqlCommandBuilder scb=new SqlCommandBuilder(sda);

If(ds.Tables[“Publisher”].Rows.Count>0)

{

ds.Tables[Publisher].Rows[0].Delete();

sda.Update(ds,”Publisher”);

}

 

 

使用Command对象更新数据

SqlConnection conn=new SqlConnection(“server=(local);Initial Catalog=BookShop;Integrated Security=true”);

Conn.Open();

String name=”我的出版社”;

String str1=”insert into Publisher(name) values(‘ ”+name+” ’)”;

SqlCommand comm1=new SqlCommand(str1,conn);

Comm1.ExecuteNonQuery();//使用Command对象添加数据

String str2=”update Publisher set name=’我的出版社’ where name=’我的旧出版社’”;

SqlCommand comm2=new SqlCommand(str2,conn);

Comm2.ExecuteNonQuery();//使用Command对象修改数据

String str3=”delete from Publisher where name=’我的出版社’”;

SqlCOmmand comm3=new SqlCOmmand(str3,conn);

Comm3.ExecuteNonQuery();//使用Command对象删除数据

Comm3.Close();

需要手工添加Open()方法打开数据库连接,Close()方法关闭数据库

相关阅读 更多 +
排行榜 更多 +
西安交大通

西安交大通

生活实用 下载
长江云通

长江云通

生活实用 下载
translatez

translatez

生活实用 下载