
如何连接sql数据库?即连接数据库命令是什么
展开全部
public SqlConnection getConn()
{
try
{
string a = AppDomain.CurrentDomain.BaseDirectory.ToString();
string s = a.Substring(0, a.Length - 1);
StreamReader sr = new StreamReader(s + "\\server.txt ", Encoding.GetEncoding("GB2312"));
string ip = sr.ReadLine().Trim();
string database = sr.ReadLine().Trim();
string sa = sr.ReadLine().Trim();
string pwd = sr.ReadLine().Trim();
// conStr = "Data Source = " + ip + ";Initial Catalog = '" + s + "'; Persist Security Info = false; User ID = sa; Password = ";
string conStr;
conStr = "Data Source = " + ip + ";Initial Catalog = " + database + "; Persist Security Info = false; User ID = "+sa +"; Password ="+pwd +" ";
conn = new SqlConnection(conStr);
conn.Open();
}
catch (Exception es)
{ } return conn;
}
{
try
{
string a = AppDomain.CurrentDomain.BaseDirectory.ToString();
string s = a.Substring(0, a.Length - 1);
StreamReader sr = new StreamReader(s + "\\server.txt ", Encoding.GetEncoding("GB2312"));
string ip = sr.ReadLine().Trim();
string database = sr.ReadLine().Trim();
string sa = sr.ReadLine().Trim();
string pwd = sr.ReadLine().Trim();
// conStr = "Data Source = " + ip + ";Initial Catalog = '" + s + "'; Persist Security Info = false; User ID = sa; Password = ";
string conStr;
conStr = "Data Source = " + ip + ";Initial Catalog = " + database + "; Persist Security Info = false; User ID = "+sa +"; Password ="+pwd +" ";
conn = new SqlConnection(conStr);
conn.Open();
}
catch (Exception es)
{ } return conn;
}
追问
最好能告诉我delphi连接的就好了
追答
你更换下语句的方式就好了阿。上边的是C#连接的方式。delphi我也不懂。
总之连接数据库方法:
conStr = "Data Source = " + ip + ";Initial Catalog = " + database + "; Persist Security Info = false; User ID = "+sa +"; Password ="+pwd +" ";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |