C# 简单的连接数据库

panyuan911 2008-04-17 02:16:11
C#中怎么使用数据库?
用本机SQL 自带的Northwind数据库执行 select count(*) from employees 就行
尽量简单一些

...全文
396 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
龙宜坡 2008-04-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mylibin 的回复:]
C# code
sqlConnection con = new sqlConnection("server=.;uid=sa;database=Northwind")
con.open();
sqlCommand cmd = new sqlCommand("select count(*) from employees",con)
sqlDataReader dr = cmd.ExecuteReader();
if(dr.Reader())
{
string count = dr[0].ToString();
}
con.close();
[/Quote]
kingmax54212008 2008-04-18
  • 打赏
  • 举报
回复
using System.Data.SqlClient;
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;database=Northwind"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from employees ", conn);
int _count = (int)cmd.ExecuteScalar();
}
exy337 2008-04-18
  • 打赏
  • 举报
回复
记得用
using System.Data.SqlClient;
exy337 2008-04-18
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 exy337 的回复:]
楼主偶的代码比较少,你看能帮到你吗.

using (SqlConnection conn = new SqlConnection("server=.;uid=sa;database=Northwind"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from employees ", conn);
int _count = (int)cmd.ExecuteScalar();
}
[/Quote]
无须关闭,using中的内容自己关闭数据库的.相对来说会安全一点.
exy337 2008-04-18
  • 打赏
  • 举报
回复
楼主偶的代码比较少,你看能帮到你吗.
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;database=Northwind"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from employees ", conn);
int _count = (int)cmd.ExecuteScalar();
}
还想懒够 2008-04-18
  • 打赏
  • 举报
回复
MSDN,搜ConnectionString就出来了
gebz456 2008-04-18
  • 打赏
  • 举报
回复
都是引用的没意思
liyin_liu 2008-04-18
  • 打赏
  • 举报
回复
jfjt
wupiero 2008-04-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mylibin 的回复:]
C# code
sqlConnection con = new sqlConnection("server=.;uid=sa;database=Northwind")
con.open();
sqlCommand cmd = new sqlCommand("select count(*) from employees",con)
sqlDataReader dr = cmd.ExecuteReader();
if(dr.Reader())
{
string count = dr[0].ToString();
}
con.close();
[/Quote]
不慕沙卡拉卡 2008-04-18
  • 打赏
  • 举报
回复
楼上的已经说的很清楚了!
提一点 可以server=(local);表示本地服务器!
weg910 2008-04-18
  • 打赏
  • 举报
回复
xuexi
bestlove2008 2008-04-18
  • 打赏
  • 举报
回复
小弟想作个数状目录,想用鼠标右键点击某个节点的时候,能够弹出一个列表框,进行选择操作菜单:就像IIS,那样!
focuswe 2008-04-17
  • 打赏
  • 举报
回复
直接在web.config中配置或者
using System.Data.SqlClient;
//.表示本地服务,也可以用127.0.0.1 代替//Northwind就是你的数据库名了
sqlConnection con = new sqlConnection("server=.;uid=sa;database=Northwind")
con.open();
sqlCommand cmd = new sqlCommand("select count(*) from employees",con)
sqlDataReader dr = cmd.ExecuteReader();
if(dr.Reader())
{
string count = dr[0].ToString();
}
con.close();
古龙老子 2008-04-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mylibin 的回复:]
C# code

using System.Data.SqlClient;
//.表示本地服务,也可以用127.0.0.1 代替//Northwind就是你的数据库名了
sqlConnection con = new sqlConnection("server=.;uid=sa;database=Northwind")
con.open();
sqlCommand cmd = new sqlCommand("select count(*) from employees",con)
sqlDataReader dr = cmd.ExecuteReader();
if(dr.Reader())
{
string count = dr[0].ToString();
}
con.close();
[/Quote]
panyuan911 2008-04-17
  • 打赏
  • 举报
回复
哈 多谢大家
我姓区不姓区 2008-04-17
  • 打赏
  • 举报
回复
不要忘了using System.Data.SqlClient;
hangang7403 2008-04-17
  • 打赏
  • 举报
回复
用SQL最好,不过也可以用OLEDBCONNECTION ,不过速度不如SQL快
eagle_2008 2008-04-17
  • 打赏
  • 举报
回复



sqlConnection con = new sqlConnection("server=.;Integrated Secutity=true";database=Northwind;);
sqlAdapter adapter= new sqlAdapter("select count(*) from employees",con);
DataSet ds=new DataSet();
adapter.Fill(ds,"employees");
MessageBox.show(ds.Tables["employees"].Rows[0][0].ToString());

这个方法也可以
problem2050 2008-04-17
  • 打赏
  • 举报
回复
支持
schoollong 2008-04-17
  • 打赏
  • 举报
回复
好多代码啊,呵呵
楼主有福了
加载更多回复(11)

110,561

社区成员

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

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

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