C#中连接数据库 C#中连接数据库有几种方法?请高手指点指点````` Thank You 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://apps.hi.baidu.com/share/detail/14973865 http://topic.csdn.net/u/20091210/15/41b751c2-1779-492d-9df9-e7e0007a4e6d.html 常用的两种,不管是什么数据库,一个是选择数据源 一个是代码实现,代码实现 sqlconnection 和 sqlcommand网上很多关于数据库连接的例子 这里就不帮你找了 (一)常用连接:1.使用SqlConnection对象:public void SqlConnectionOpen(){ SqlConnection conn= new SqlConnection(); conn.ConnectionString = "user id=sa;password=;initial catalog=northwind;datasource=localhost;connect Timeout=20"; conn.Open();}2.使用OleDbConnection对象:public void OleDBConnectionOpen(){ OleDBConnection conn = new OleDbconnection(); conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Customer.mdb"; conn.Open();}(二)其它:1.ODBC连接Access本地数据库 conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=C:\a.mdb;"+ "Uid=Admin;"+"Pwd=;");2.ODBC连接Access系统数据库 conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=C:\a.mdb;"+ "SystemDB=Admin;"+"Pwd=;");3.ODBC连接Access系统数据库 conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=\\server\share\a.mdb;");4.ODBC连接Excel系统数据库 conGoodDay.Open("Driver={Microsoft Access Driver(*.xls)};"+"DriverId=790;"+ "Dbq=C:\a.xls;"+"DefaultDir=c:\somepath;");5.ODBC连接Oracle系统数据库 conGoodDay.Open("Driver={Microsoft ODBC for Oracle};"+"Server=OracleServer.world;"+ "Uid=Admin;"+"Pwd=password;");6.ODBC连接Sql Servr conGoodDay.Open("Driver={Sql Server};"+"Server=myServer;"+"Database=myDatabaseName;" "Uid=Admin;"+"Pwd=password;");7.ODBC连接Visual FoxPro conGoodDay.Open("Driver={Microsoft Visual FoxPro Driver};"+ "SourceType=DBC;"+"SourceDB=c:a.dbc;"+"Exclusive=No;"); 简单的数据库连接就是一段代码:string connStr = "Data Source=.; Initial Catalog=liushouakang; User Id=sa; Pwd=qing5215107"; SqlConnection conn = new SqlConnection(connStr); try { string sql = "select* from Student"; SqlCommand comm = new SqlCommand(sql, conn); conn.Open(); SqlDataReader reader = comm.ExecuteReader(); int index = 0; while (reader.Read()) { Emploee ee = new Emploee(); ee.Id1 = Convert.ToInt32(reader[0]); ee.Name1 = reader[1].ToString(); ee.Age = Convert.ToInt32(reader[2]); ee.Sex = Convert.ToString(reader[3]); ee.Money = Convert.ToInt32(reader[4]); ee.Email = reader[5].ToString(); ees[index] = ee; index++; } reader.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { conn.Close(); } return ees; }这是个方法,,下面的自己来 .net开发的网页,附件上传大小如何检查? 关于treeview的单击节点事件 调试出现错误,为何? 有关datagrid的问题 如何修改dataset数据集中某一列的数据(winform)?急!在线等! 在From1窗体有Button,点击Button如何使From1隐藏,而显示From2?(急急急) 服务程序的问题 两个ArrayList 如何合成一个 求高效的随机数产生算法(在线等) .net中xml操作的问题,救命啊!!! C# 中要怎么检测本机 是否 通内外网 comboBox.SelectedIndex的问题:下拉列表中选择好了,但是selectedindex值不变!
1.使用SqlConnection对象:
public void SqlConnectionOpen()
{
SqlConnection conn= new SqlConnection();
conn.ConnectionString = "user id=sa;password=;initial catalog=northwind;datasource=localhost;connect Timeout=20";
conn.Open();
}
2.使用OleDbConnection对象:
public void OleDBConnectionOpen()
{
OleDBConnection conn = new OleDbconnection();
conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Customer.mdb";
conn.Open();
}
(二)其它:
1.ODBC连接Access本地数据库
conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=C:\a.mdb;"+
"Uid=Admin;"+"Pwd=;");
2.ODBC连接Access系统数据库
conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=C:\a.mdb;"+
"SystemDB=Admin;"+"Pwd=;");
3.ODBC连接Access系统数据库
conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=\\server\share\a.mdb;");
4.ODBC连接Excel系统数据库
conGoodDay.Open("Driver={Microsoft Access Driver(*.xls)};"+"DriverId=790;"+
"Dbq=C:\a.xls;"+"DefaultDir=c:\somepath;");
5.ODBC连接Oracle系统数据库
conGoodDay.Open("Driver={Microsoft ODBC for Oracle};"+"Server=OracleServer.world;"+
"Uid=Admin;"+"Pwd=password;");
6.ODBC连接Sql Servr
conGoodDay.Open("Driver={Sql Server};"+"Server=myServer;"+"Database=myDatabaseName;"
"Uid=Admin;"+"Pwd=password;");
7.ODBC连接Visual FoxPro
conGoodDay.Open("Driver={Microsoft Visual FoxPro Driver};"+
"SourceType=DBC;"+"SourceDB=c:a.dbc;"+"Exclusive=No;");
string connStr = "Data Source=.; Initial Catalog=liushouakang; User Id=sa; Pwd=qing5215107";
SqlConnection conn = new SqlConnection(connStr);
try
{
string sql = "select* from Student";
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
int index = 0;
while (reader.Read())
{
Emploee ee = new Emploee();
ee.Id1 = Convert.ToInt32(reader[0]);
ee.Name1 = reader[1].ToString();
ee.Age = Convert.ToInt32(reader[2]);
ee.Sex = Convert.ToString(reader[3]);
ee.Money = Convert.ToInt32(reader[4]);
ee.Email = reader[5].ToString();
ees[index] = ee;
index++;
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
return ees;
}
这是个方法,,下面的自己来