C#中数据库的连接

类别:.NET开发 点击:0 评论:0 推荐:

这是我连接sql数据库的部分代码:
  private void Form1_Load(object sender, System.EventArgs e)
  {
   string source="server=127.0.0.1;"+"uid=sa;pwd=sa;"+"database=DatabaseName";
   string select ="select * from TableName";
   SqlConnection conn = new SqlConnection(source);
   SqlDataAdapter da= new SqlDataAdapter(select,conn);
   DataSet ds= new DataSet();
   da.Fill(ds,"TableName");
   dataGrid1.SetDataBinding(ds,"TableName"); 
    }



应该还可以优化的,却不知道怎么优化,望指教。


   string strConn= "Provider=Microsoft.Jet.OLEDB.4.0;"+"Password=;User ID=Admin;"+"data source=\\192.168.0.22"+"inital catalog=student.mdb";
   //string strConn="Provider = Microsoft.Jet.OLEDB.4.0; Data Source=192.168.0.22";
   OleDbConnection myConn=new OleDbConnection(strConn);
   myConn.Open();
   string strInsert="INSERT INTO uuser(uuser,password,userid) VALUES(";
   strInsert+=textBox1.Text+",'";
   strInsert+=textBox2.Text+"''";
   strInsert+=textBox3.Text+")";
   OleDbCommand inst=new OleDbCommand(strInsert,myConn);
   inst.ExecuteNonQuery();
   myConn.Close();
连接access的。

本文地址:http://com.8s8s.com/it/it43842.htm