简体   繁体   English

连接字符串在C#windows应用程序中不起作用

[英]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语句来配置你的SqlConnectionSqlCommand类的;

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.

相关问题 C#windows应用程序中的连接字符串 - Connection String in C# windows application 如何在C#Windows应用程序中为整个应用程序设置连接字符串? - How to set connection string for whole application in c# windows Application? Windows应用程序数据库连接c# - windows Application Database Connection c# C#Windows应用程序:使用Windows身份验证连接到远程SQL Server 2008的连接字符串 - C# Windows application: connection string to connect to remote sql server 2008 using windows authentication 回收 Windows Forms C# 中的连接字符串 - Recycling Connection String In Windows Forms C# C#应用程序的个性化数据库连接字符串 - Personalized database Connection string for C# application 如何使用数据库部署C#Windows应用程序(安装文件)以及如何在客户端计算机上管理连接字符串 - How to deploy C# windows application (setup file) with database and how to manage connection string on client machine 无法从C#连接字符串(Windows应用程序)中的本地服务器位置加载数据库 - Unable to load database from local server location in C# Connection String (windows application) 在运行时建立连接字符串,并将其保存在C#Windows应用程序的应用程序设置中 - build connection string at runtime and save the same in application setting on c# windows app 如何在C#Windows应用程序中的一个位置写入数据库连接字符串 - How to write database connection string in one location in C# windows application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM