简体   繁体   English

将记录从asp.net插入SQL Server数据库期间出错

[英]Error during insert of record from asp.net into SQL Server database

I got this error during insert of data into a SQL Server database 在将数据插入SQL Server数据库期间出现此错误

Here is my code in button click event 这是我在按钮单击事件中的代码

try
{
    string ConnString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=\\MOD03-PC\\Share Folder mod03\\amts\\amtsfuelconsuption\\AmtsFuelConsumption\\AmtsFuelConsumption\\App_Data\\AmtsDatabse.mdf;Integrated Security=True;Connect Timeout=900,providerName=System.Data.SqlClient";  
    SqlConnection con = new SqlConnection(@ConnString);

    SqlCommand cmd = new SqlCommand("InsertBodyTypeMaster", con);
    cmd.CommandTimeout = 0;
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.AddWithValue("bodytypename", txtBTname.Text.ToString());

    con.Open();

    int k = cmd.ExecuteNonQuery();

    if (k != 0)
    {
        lblmessage.Text = "Record Inserted Succesfully into the Database";
        lblmessage.ForeColor = System.Drawing.Color.CornflowerBlue;
    }

    con.Close();
    con.Dispose();
}
catch (Exception ex)
{
    lblmessage.Text = ex.ToString();
}

I see a few things wrong; 我发现有些错误;

  • As mentioned, you need to change your Connect Timeout=900, to Connect Timeout=900; 如前所述,您需要Connect Timeout=900,更改为Connect Timeout=900;
  • You need to delete providerName=System.Data.SqlClient part since you already using the .NET provider for SQL Server. 您需要删除providerName=System.Data.SqlClient部分,因为您已经将.NET提供程序用于SQL Server。 Provider names for .NET are implicit based on the implementing class and not needed to specified in the connection string. .NET的提供程序名称基于实现类是隐式的, 不需要在连接字符串中指定。 When you delete this, you will not need ; 删除时,您将不需要; at the end of Connect Timeout=900; Connect Timeout=900;结束时Connect Timeout=900; anymore 不再
  • Use using statement to dispose your connection and command automatically instead of calling Close or Dispose methods manually. 使用using语句自动处理连接和命令,而不是手动调用CloseDispose方法。
  • Don't use AddWithValue as much as you can. 尽量不要使用AddWithValue It may generate unexpected and surprising results sometimes . 有时它可能会产生意外和令人惊讶的结果 Use Add method overload to specify your parameter type and it's size. 使用Add方法重载来指定您的参数类型及其大小。

Final connection string should be as; 最终连接字符串应为;

string ConnString = "Data Source=(LocalDB)\v11.0,AttachDbFilename=\\MOD03-PC\\Share Folder mod03\\amts\\amtsfuelconsuption\\AmtsFuelConsumption\\AmtsFuelConsumption\\App_Data\\AmtsDatabse.mdf;Integrated Security=True;Connect Timeout=900"; 

连接字符串中的连接超时属性中的900后面有逗号而不是分号。

Cause your connection string is total weird. 因为您的连接字符串很奇怪。 remove those ; 删除那些; and replace them with , . 并与替换它们, Also, make sure you spell them properly. 另外,请确保正确拼写它们。 It should be like 应该像

string ConnString = "Data Source=(LocalDB)\v11.0,AttachDbFilename=\\MOD03-PC\\Share Folder mod03\\amts\\amtsfuelconsuption\\AmtsFuelConsumption\\AmtsFuelConsumption\\App_Data\\AmtsDatabse.mdf,Integrated Security=True,Connect Timeout=900;providerName=System.Data.SqlClient"; 

Also the below line 还有下面一行

SqlConnection con = new SqlConnection(@ConnString); 

It should be 它应该是

SqlConnection con = new SqlConnection(ConnString);

You are calling Dispose() inside try block which is big blunder as shown below. 您正在try块内调用Dispose() ,这是一个大错误,如下所示。 Either use Using(...) block (or) finally block 使用Using(...)阻止(或) finally阻止

try
{
 ....
    con.Close();
    con.Dispose();
}

Should be 应该

finally
{
    con.Close();
    con.Dispose();
}

Looks like it's time you should start reading through documentation. 看起来是时候开始阅读文档了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM