[英]Connection string not working in C# windows application
For some reasons, I am unable to establish a data connection using my connection string. 由于某些原因,我无法使用我的连接字符串建立数据连接。 I was doing with the below code 我正在使用以下代码
var connectionString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection(connectionString);
cmd.Connection = connectionString;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = " dbo.SelectAll";
SqlParameter param = new SqlParameter("@tradeDate","201401");
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
cmd.Parameters.Add(param);
But for some reasons when I am initializing the connection property to my command using cmd.Connection = connectioString
, is is throwing an exception as follows 但由于某些原因,当我使用cmd.Connection = connectioString
将连接属性初始化为我的命令时,是抛出异常,如下所示
Cannot implicitly convert type 'string' to 'System.Data.SqlClient.SqlConnection' 无法将类型'string'隐式转换为'System.Data.SqlClient.SqlConnection'
I think you need just 我想你需要
cmd.Connection = con;
You are try to do set your SqlCommand.Connection
property with your connection string. 您尝试使用连接字符串设置SqlCommand.Connection
属性。 But this property is for specify your SqlConnection
object, not your connection string. 但是此属性用于指定您的SqlConnection
对象,而不是您的连接字符串。
From documentation; 从文件;
Gets or sets the SqlConnection used by this instance of the SqlCommand. 获取或设置此SqlCommand实例使用的SqlConnection 。
And since there is no implicit conversation from SqlConnection to string, that's why you get compile time error. 并且由于没有从SqlConnection到字符串的隐式对话,这就是编译时错误的原因。
As a side note, use using
statement to dispose your SqlConnection
and SqlCommand
like; 作为旁注,使用using
语句来配置你的SqlConnection
和SqlCommand
类的;
using(SqlConnection con = new SqlConnection(connectionString))
using(SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
...
...
}
or you can use SqlConnection.CreateCommand()
method to create your SqlCommand
associated your SqlConnection
inside your using statement like; 或者您可以使用SqlConnection.CreateCommand()
方法在您的using语句中创建与SqlConnection
关联的SqlCommand
;
using(SqlConnection con = new SqlConnection(connectionString))
using(SqlCommand cmd = con.CreateCommand())
{
...
...
}
You're confusing the connection string needed to connect to the database and the actual SqlConnection. 您混淆了连接数据库和实际SqlConnection所需的连接字符串。
try this (for your specific code): 试试这个(针对您的具体代码):
cmd.Connection = con;
According to MSDN here is a proper example: 根据MSDN,这是一个恰当的例子:
private static void CreateCommand(string queryString, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
Link to original article: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx 链接到原始文章: http : //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.