什么数据库,Access如下:
//加载记录从数据库
OleDbConnection myConn=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\CSharp3\QXKMeTone\bin\Debug\First.mdb;");
myConn.Open();
OleDbDataAdapter thisAdapter= new OleDbDataAdapter("SELECT * FROM Student",myConn);
OleDbCommandBuilder thisBuilder=new OleDbCommandBuilder(thisAdapter);
DataSet thisDataSet=new DataSet();
thisAdapter.Fill(thisDataSet,tablename);
int nTotals=thisDataSet.Tables[tablename].Rows.Count;
Student student=new Student();
for(int i=0;i<nTotals;i++)
{
Sql Server
首先取得数据,放到DataGrid里System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"student");然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"student");
如果需要,可以绑定TextBox来作录入,而用DataGrid显示
this.textBox16.DataBindings.Add("Text",dt,"student.stuno");
然后进行数据的操作如:
增加:
this.BindingContext[dt,"student"].AddNew();
删除:
Oracle
string  ShcameName = ConfigurationSettings.AppSettings["source"];
string  ServiceName = ConfigurationSettings.AppSettings["id"];
string service=ConfigurationSettings.AppSettings["catalog"];
cons="User ID="+ShcameName.ToString()+";password="+service+";Data Source="+ServiceName.ToString()+";Persist Security Info=False";
myconnection.ConnectionString = cons;