错误消息:初始化字符串的格式不符合从索引 0 开始的规范
error message: Format of the initialization string does not conform to specification starting at index 0
我搜索了 SO 但没有找到解决我的错误的方法。
我正在使用 VS 2013 和 SQL Server 2014。
下面是我的连接字符串:
using (SqlConnection sqlConnection = new SqlConnection("cnInvestTracker"))
{
}
我的 web.config 是:
<connectionStrings>
<add name="cnInvestTracker"
connectionString="Data Source=localhost;Initial Catalog=InvestTracker;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
当代码执行 using 行时,我收到一条错误消息。错误信息是:
Format of the initialization string does not conform to specification
starting at index 0.
导致错误的原因是什么?
值 "cnInvestTracker"
本身 不是有效的连接字符串。这就是您要在此处使用的内容:
new SqlConnection("cnInvestTracker")
该构造函数不需要连接字符串的 name,它需要 the connection string itself:
new SqlConnection(ConfigurationManager.ConnectionStrings["cnInvestTracker"].ConnectionString)
(您可能必须添加对 System.Configuration
的引用,并且您可能需要添加一些错误检查以确保在尝试引用它之前存在具有该名称的连接字符串。)
using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["cnInvestTracker"].ConnectionString))
{
}
一旦建立连接,您可能会想用代码做更多的事情,所以下面是一个示例,说明如果您想要 return 数据,例如使用 .Fill()
SqlDataAdapter sda;
DataTable someDataTable = new DataTable();
using (SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["cnInvestTracker"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("ups_GeMyStoredProc", connStr)) //replace with your stored procedure. or Sql Command
{
cmd.CommandType = CommandType.StoredProcedure;
sda = new SqlDataAdapter(cmd);
new SqlDataAdapter(cmd).Fill(someDataTable);
}
}
我搜索了 SO 但没有找到解决我的错误的方法。
我正在使用 VS 2013 和 SQL Server 2014。
下面是我的连接字符串:
using (SqlConnection sqlConnection = new SqlConnection("cnInvestTracker"))
{
}
我的 web.config 是:
<connectionStrings>
<add name="cnInvestTracker"
connectionString="Data Source=localhost;Initial Catalog=InvestTracker;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
当代码执行 using 行时,我收到一条错误消息。错误信息是:
Format of the initialization string does not conform to specification starting at index 0.
导致错误的原因是什么?
值 "cnInvestTracker"
本身 不是有效的连接字符串。这就是您要在此处使用的内容:
new SqlConnection("cnInvestTracker")
该构造函数不需要连接字符串的 name,它需要 the connection string itself:
new SqlConnection(ConfigurationManager.ConnectionStrings["cnInvestTracker"].ConnectionString)
(您可能必须添加对 System.Configuration
的引用,并且您可能需要添加一些错误检查以确保在尝试引用它之前存在具有该名称的连接字符串。)
using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["cnInvestTracker"].ConnectionString))
{
}
一旦建立连接,您可能会想用代码做更多的事情,所以下面是一个示例,说明如果您想要 return 数据,例如使用 .Fill()
SqlDataAdapter sda;
DataTable someDataTable = new DataTable();
using (SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["cnInvestTracker"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("ups_GeMyStoredProc", connStr)) //replace with your stored procedure. or Sql Command
{
cmd.CommandType = CommandType.StoredProcedure;
sda = new SqlDataAdapter(cmd);
new SqlDataAdapter(cmd).Fill(someDataTable);
}
}