连接数据库
public static SqlConnection CreateConn()
{
SqlConnection conn = new SqlConnection( "server=.;database=OQC;uid=sa;pwd=; ");
return conn;
}
这个是本地连接,,, 怎么样才能连接服务器的数据库啊??
------解决方案--------------------SqlConnection conn = new SqlConnection( "server=XXX.XXX.XXX.XXX;database=OQC;uid=sa;pwd=; ");
------解决方案--------------------//要连接数据库服务器的IP地址
string strIp = "1.1.1.1 ";
//数据库服务器上数据库服务的端口号
string strPort = "1433 ";
//要连接的数据库名
string strDataBase = "myDB ";
//使用的数据库用户
string strUid = "myId ";
//密码
string strPwd = "myPwd ";
//连接数据库字符串
string strConn = "Data Source= "+strIp+ ", "+strPort+ ";Network Library=DBMSSOCN;Initial Catalog= "+strDataBase+ ";User ID= "+strUid+ ";Password= "+strPwd
//数据库连接
SqlConnection conn = new SqlConnection(strConn);
try
{
conn.open();
conn.close();
//连接成功
}
catch
{
//连接失败
}