【发布时间】:2019-02-02 03:52:46
【问题描述】:
尝试使用 Visual Studio 2017 访问 SQL Server 2014 数据库;在每次运行时,它都会显示此错误
System.ArgumentException:初始化字符串的格式不符合从索引 124 开始的规范
阅读一些关于堆栈溢出的问题,但它们属于其他上下文。比如 asp.net 或者 azure 数据库等
<configuration>
<connectionStrings>
<add name="EMPLOYES"
connectionString="Data Source=DESKTOP-0ROOGH3\SQLEXPRESS;Initial Catalog=EMPLOYES;Integrated Security = true;System.Dat.SqlClient"/>
</connectionStrings>
</configuration>
代码:
private void button2_Click(object sender, EventArgs e)
{
String conString = ConfigurationManager.ConnectionStrings["EMPLOYES"].ConnectionString;
// here I get the exception
SqlConnection conn = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("AddNewEmployee", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@EmployeeID", SqlDbType.NVarChar, 8).Value = EmpIDTextBox.Text;
cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50).Value = FirstNameTextBox.Text;
cmd.Parameters.Add("@LastNmae", SqlDbType.NVarChar, 50).Value = LastNameTextBox.Text;
cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = EmailTextBox.Text;
cmd.Parameters.Add("@Telephone", SqlDbType.NVarChar, 50).Value = TelephoneTextBox.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Employee is added successfully!");
}
我希望插入记录,但它会产生一些异常
【问题讨论】:
-
我非常怀疑连接字符串是否可以处理参数中的新行和回车,这是它在 app.config 中的真实外观吗?
-
(加上
System.Dat.SqlClient看起来很可疑) -
@Alexei 是的,我明白了,现在效果很好
标签: c# ado.net sql-server-2014-express