与c#连接sql_c与cc性能比较
与c#连接sql由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c与cc性能比较”。
C#连接SQL Server
string strConnection=“uid=sa;paword=;”;
strConnection+=“initial catalog=Northwind;Server=YourSQLServer;”;
strConnection+=“Connect Timeout=30”;
SqlConnection objConnection=new SqlConnection(strConnection);
..objConnection.Open();
objConnection.Close();
“uid=sa”:连接数据库的用户名为sa.“paword=”:连接数据库的验证密码为空.他的别名为“pwd”,所以我们可以写为“pwd=”.“initial catalog=Northwind”:使用的数据源为“Northwind”这个数据库.他的别名为“Database”,本句可以写成“Database=Northwind”.“Server=YourSQLServer”:使用名为“YourSQLServer”的服务器.他的别名为“Data Source”,“Addre”,“Addr”.“ Connect Timeout=30”:连接超时时间为30秒.(根据情况添加)
PS:
1.你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用“uid”和“paword”这样的方式来登录,而需要使用
“Trusted_Connection=SSPI”来进行登录.2.如果使用的是本地数据库且定义了实例名,则可以写为“Server=(local)实例名”;如果是远程服务器,则将“(local)”替换为远程服务器的名称或IP地址.