c#如何连接sql,要代码

希望能有全的代码,谢谢各位

【C#连接SQL Server】
程序代码:
using System.Data;
using System.Data.SqlClient; //使用的命名空间

string connectionString=”Server(Data Source)=服务器名;initial catalog(Database)=数据库名;user id(uid)=用户名;password(pwd)=密码;(Windows登陆模式:Trusted Connection=SSPI);Connect Timeout=30”;
//创建数据库连接字符串

SqlConnection connectionObj=new SqlConnection(connectionString); //建立连接对象
connectionObj.Open(); //连接打开
/*创建SqlCommand等进行数据库操作*/
connectionObj.Close();//连接关闭

解释:
“user id”:连接数据库的验证用户名。
“password=”:连接数据库的验证密码。
这里注意,你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录。如果你的SQL Server设置为Windows登录,那么在这里就不需要使用“user id”和“password”这样的方式来登录,而需要使用“Trusted_Connection=SSPI”来进行登录。
“initial catalog=”:使用的数据源。
“Server=”:使用的服务器。
“Connect Timeout=30”:连接超时时间为30秒。
在这里,建立连接对象用的构造函数为:SqlConnection。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-18
以下是C#中的各种数据库的连接方式,VB.NET中类似,只是变量定义的方式不一样而已.
(一)常用连接:
1.使用SqlConnection对象:
a. Sql 验证
public void SqlConnectionOpen()
{
SqlConnection conn= new SqlConnection();
conn.ConnectionString = "user id=sa;password=;initial catalog=northwind;datasource=localhost;connect Timeout=20";
conn.Open();
}
b. Windows 身份验证
public void SqlConnectionOpen()
{
SqlConnection conn= new SqlConnection();
conn.ConnectionString = "Integrated Security=SSPI;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;");

Windows 身份验证
建议使用 Windows 身份验证(有时也称为“集成安全性”)连接到支持其的数据源。连接字符串中使用的语法根据提供程序的不同而不同。下表演示用于 .NET Framework 数据提供程序的 Windows 身份验证语法。
提供程序
语法
SqlClient
Integrated Security=true;
-- or --
Integrated Security=SSPI;
OleDb
Integrated Security=SSPI;
Odbc
Trusted_Connection=yes;
OracleClient
Integrated Security=yes;
Integrated Security=true 用于 OleDb 提供程序时会引发异常。
第2个回答  2011-09-18
"server=.;database=数据库名;uid=用户名;pwd=密码"
这种方式比较简单,
还有一种,好像比较长,我也忘了。
第3个回答  2011-09-18
public string connectionString =“server=.;database=dataName;uid=sa;pwd=sa;”
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand();
相似回答