给同学们了一趟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.

  

 

上一篇:Mathematical Contest in Modeling---Traffic Circle Problem


下一篇:[罗振辉]MFC中菜单命令路由方式