[英]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; 我发现有些错误;
Connect Timeout=900,
to Connect Timeout=900;
Connect Timeout=900,
更改为Connect Timeout=900;
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. ;
;
at the end of Connect Timeout=900;
Connect Timeout=900;
结束时Connect Timeout=900;
anymore using
statement to dispose your connection and command automatically instead of calling Close
or Dispose
methods manually. using
语句自动处理连接和命令,而不是手动调用Close
或Dispose
方法。 AddWithValue
as much as you can. AddWithValue
。 It may generate unexpected and surprising results sometimes . 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.