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

时间:2014-10-06 09:28:40

标签: c# asp.net sql-server connection-string

我在SSMS中创建了一个登录,并在Web.config中添加了一个connectionString,如下所示:

<add name="KhadamatConnectionString"
         connectionString="data source=.;Database=AcaService; uid=admin pwd='mypass'"
         providerName="System.Data.SqlClient"/>         

我收到以下错误:

初始化字符串的格式不符合从索引处开始的规范说明:在执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。      异常详细信息:System.ArgumentException:初始化字符串的格式不符合从索引35开始的规范。


这是我提交一些数据的代码:

protected void btnSend_Click(object sender, EventArgs e)
{
    string connectionString=ConfigurationManager.ConnectionStrings["KhadamatConnectionString"].ConnectionString;
    SqlConnection sc = new SqlConnection(connectionString);
    sc.Open();
    if (sc.State == ConnectionState.Closed)
    {
        lblMsg.ForeColor = System.Drawing.Color.Red;
        lblMsg.Text = "خطای ارتباط با پایگاه داده";
        return;
    }
    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.Connection = sc;            
    sqlCmd.CommandType = CommandType.Text;
    sqlCmd.CommandText="INSERT INTO tblComment VALUES(@name,@email,@comment)";
    sqlCmd.Parameters.Add(new SqlParameter("@Name",System.Data.SqlDbType.NVarChar));
    sqlCmd.Parameters.Add(new SqlParameter("@Email",System.Data.SqlDbType.NVarChar));
    sqlCmd.Parameters.Add(new SqlParameter("@Comment",System.Data.SqlDbType.NVarChar));
    sqlCmd.Parameters["@Name"].Value=txtName.Text;
    sqlCmd.Parameters["@Email"].Value=txtEmail.Text;
    sqlCmd.Parameters["@Comment"].Value=txtComment.Text;
    SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    lblMsg.ForeColor = System.Drawing.Color.Green;
    lblMsg.Text="اطلاعات ذخیره شد";
    sc.Close();
}

1 个答案:

答案 0 :(得分:0)

像这样更改连接字符串

<appSettings>
    <add key="KhadamatConnectionString" 
    value="Server=[copy name from Your ServerName of SSMS];Database=AcaService;uid=admin;pwd=mypass;" />
</appSettings>

<connectionStrings>
    <add name="KhadamatConnectionString" connectionString="Data Source=[copy name from Your ServerName of SSMS];Initial Catalog=AcaService;User ID=admin;Password=mypass;" providerName="System.Data.SqlClient"/>
    </connectionStrings>