给同学们了一趟C#数据库操作课程

ADO.NET基本的操作步骤

         简单说明: ADO.NET其实是一个很大的类库.其对数据库的操作也就是对这个类属性和方法的调用.

         1、连接并打开数据库(SQL Server2000为例)

          SqlConnection objConnection= new SqlConnection("Data Source=WBOY;Initial Catalog=stuinfo;Persist Security Info=True;User ID=sa;Password=XXX");

         连接字: Data Source=WBOY;Initial Catalog=stuinfo;Persist Security Info=True;User ID=sa;Password=XXX

          objConnection.Open();

         根据系统采用的数据不同连接类也不同的.

     SqlConnection

     OleDbConnection

     OracleConnection

         2、执行SQL语句

SqlCommand objComm=new SqlCommand();

objComm.CommandText="SQL 语句";

objComm.CommandType=CommandType.Text ;

objComm. Connection=objConnection;

objComm.ExecuteNonQuery();

 

@一般我们用在数据的InSertUpdate操作

 

3SqlDataAdapter

         SqlDataAdapter是建立在DataSet和数据源之间的一个通道.有了这个通道我们可以对数据库进行SelectCommand(查询), InsertCommand(插入), DeleteCommand(删除), UpdateCommand(更新)

 

         例如通过SelectCommand将数据获取到DataSet中去。

 

         SqlDataAdapter .doc

         SqlDataAdapter adapter = new SqlDataAdapter(SELECT    UID,Uname FROM MyDatabase WHERE UID=2", objConnection);

                         DataSet daa= new DataSet();

                         adapter.Fill(daa);

textBox1.Text = daa.Tables[0].Rows[0].ItemArray[0].ToString();

textBox2.Text = daa.Tables[0].Rows[0].ItemArray[1].ToString();

 

 

 

 

          5、关闭数据库

          da.Close()

 

附录一SqlDataAdapter .doc

 

 

SqlDataAdapter

The SqlDataAdapter serves as a bridge between a DataSet and data source. It is used for retrieving data from, and saving data to, the data source. The SqlCeDataAdapter provides this bridge by using Fill to load data from the data source into the DataSet, and using Update to send changes made in the DataSet back to the data source.

When the SqlCeDataAdapter fills a DataSet, it creates the necessary tables and columns for the returned data if they do not already exist. However, primary key information will not be included in the implicitly created schema unless the MissingSchemaAction property is set to AddWithKey. You can also have the SqlCeDataAdapter create the schema of the DataSet, including primary key information, before filling it with data by using FillSchema.

The SqlCeDataAdapter includes the SelectCommand, InsertCommand, DeleteCommand, UpdateCommand, and TableMappings properties to facilitate the loading and updating of data.

When you create an instance of SqlCeDataAdapter, properties are set to their initial values. For a list of these values, see the SqlCeDataAdapter constructor.

  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值