C#中连接数据库

will_pengwq 2011-04-08 08:58:59
C#中连接数据库有几种方法?
请高手指点指点`````
Thank You
...全文
108 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
night09876 2013-11-16
  • 打赏
  • 举报
回复
简单的数据库连接就是一段代码: 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; } 这是个方法,,下面的自己来
qq514418353 2011-04-09
  • 打赏
  • 举报
回复
(一)常用连接:
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;");
xinyuan178 2011-04-09
  • 打赏
  • 举报
回复
看你要连接哪种类型的了
qq514418353 2011-04-09
  • 打赏
  • 举报
回复
很多种
chuanzhang5687 2011-04-09
  • 打赏
  • 举报
回复
常用的两种,不管是什么数据库,一个是选择数据源 一个是代码实现,代码实现 sqlconnection 和 sqlcommand网上很多关于数据库连接的例子 这里就不帮你找了
gaocy1 2011-04-09
  • 打赏
  • 举报
回复
学习。。。。。。
monami1324 2011-04-08
  • 打赏
  • 举报
回复
我只知道两种的,一种VS自带的,一种是靠代码实现的
快乐大法师 2011-04-08
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20091210/15/41b751c2-1779-492d-9df9-e7e0007a4e6d.html
快乐大法师 2011-04-08
  • 打赏
  • 举报
回复
http://apps.hi.baidu.com/share/detail/14973865
快乐大法师 2011-04-08
  • 打赏
  • 举报
回复
连接什么数据库啊
秦剑 2011-04-08
  • 打赏
  • 举报
回复
就是种吧,只是连接不同数据库用的类不一样而已。

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧