C#中的数据库连接字符串问题 [英] Database Connection string problem in c#
本文介绍了C#中的数据库连接字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void button1_Click(object sender, EventArgs e)
{
con = new SqlConnection();
string q = "Data Source=TOSHIBA-PC;Initial Catalog=ACCESSCONTROL;User ID=ABC;Password=hello;MultipleActiveResultSets=True";
cmd = new SqlCommand (q,con);
try
{
con.Open();
MessageBox.Show("Connection opened.");
}
catch (SyntaxErrorException a)
{
MessageBox.Show("Error: " + a);
}
finally
{
con.Close();
MessageBox.Show("Connection closed.");
}
}
错误:ConnectionString属性尚未初始化.
Error : The ConnectionString property has not been initialized.
推荐答案
请在此处查看: ^ ].您会发现自己做错了.
Please look here: http://msdn.microsoft.com/en-us/library/aa326257(v=VS.71).aspx[^]. You''ll see what you were doing wrong.
private void button1_Click(object sender, EventArgs e)
{
String connectionString = "Data Source=TOSHIBA-PC;Initial Catalog=ACCESSCONTROL;User ID=ABC;Password=hello;MultipleActiveResultSets=True";
con = new SqlConnection(connectionString); // You'll have to initialize the connection with the connectionString
cmd = new SqlCommand(con);
try
{
con.Open();
MessageBox.Show("Connection opened.");
}
catch (SyntaxErrorException a)
{
MessageBox.Show("Error: " + a);
}
finally
{
con.Close();
MessageBox.Show("Connection closed.");
}
}
最好的问候,
Best Regards,
了解连接字符串
connectionstrings.com
使用连接字符串
存储和检索连接字符串
保护连接字符串
希望您能找到解决方案.
To know about connection strings
connectionstrings.com
Working with Connection Strings
Storing and Retrieving Connection Strings
Securing Connection Strings
Hope you will find solution.
这将帮助您如何逐行获取数据库连接,甚至获取连接字符串.
http://www.homeandlearn.co.uk/csharp/csharp_s12p1.html [ ^ ]
This will help you how to get connection to database line by line, even getting connection string.
http://www.homeandlearn.co.uk/csharp/csharp_s12p1.html[^]
这篇关于C#中的数据库连接字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文