初始化字符串的格式不符合从索引124开始的规范

时间:2019-02-02 03:52:47

标签: c# ado.net sql-server-2014-express

尝试使用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!");
}

我希望插入记录,但是会产生一些异常

1 个答案:

答案 0 :(得分:1)

您的连接字符串看起来完全乱码,尝试类似

<configuration>
    <connectionStrings>

       <add name="EMPLOYES" 
            connectionString="Data Source=DESKTOP-0ROOGH3\SQLEXPRESS;Initial Catalog=EMPLOYES;Integrated Security = true;" 
            providerName="System.Data.SqlClient" />

    </connectionStrings>
</connectionStrings>

其他资源