?不知所措? ExecuteNonQuery在调试模式下执行我的Insert语句3次 [英] ?At a loss? ExecuteNonQuery executing my Insert Statement 3 times in debug mode

查看:78
本文介绍了?不知所措? ExecuteNonQuery在调试模式下执行我的Insert语句3次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来很奇怪,但我不知所措。我打电话给一个简单的

函数打开一个连接到SQL Server 2000数据库并执行

一个插入语句。


private void AddMinimunWageStipen(string payrollid,double amount)

{


System.Data.SqlClient.SqlConnection cn = null;

System.Data.SqlClient.SqlCommand cm = null;


string sql =" INSERT INTO tblStipen(EmployeeID,StipenTypeID,Units,

金额,AddHours, PayrollID)" ;;

sql + =" VALUES(" + _employeeid +",3,1," + amount +",0," + payrollid

+")" ;;


试试

{

cn = new

System.Data.SqlClient.SqlConnection(PayrollSystem.Data.SQLSERVER);

cn.Open();

cm = new System.Data.SqlClient.SqlCommand(sql ,cn);

int flag = cm.ExecuteNonQuery();

cn.Close();

}

catch(例外情况)

{

thro w ex;

}

终于

{

cm.Dispose();

cn.Dispose();

}


}


当我在调试器中运行此代码时当我执行int flag = cm.ExecuteNonQuery();行时,执行此功能。插入声明

被激活3次。所以我在数据库中有相同的记录3次??

我知道代码只被激活了一次。使用相同的参数传入函数时,函数不会被调用3次。我也没有做任何特殊线程的



如果我在不停止此功能的情况下运行程序,则代码的行为类似于

它应该只记录一次。


是有什么我想念的吗?还有其他人遇到过这个问题吗?


提前致谢,


Mike R

解决方案

在单步执行处理

datareader的读取方法的代码时,我发现了类似的行为。我发现只要调试器是

要求我提供有关数据的任何信息(即监视器的手表,汽车和本地

窗口),它会导致datareader调用它的读取

方法,因为它必须尝试获取一些数据供我查看,而

调试。


听起来命令对象的ExecuteNonQuery方法在调试器中的作用与

相同。这种行为令人不安,因为它基本上是b $ b意味着你无法调试(具有任何确定性)某些

的陈述。

" ;麦克" <先生******** @ NOSPAMATALLcalibrus.com>在留言中写道

news:uH ************** @ TK2MSFTNGP11.phx.gbl ...

我知道这听起来很奇怪但我不知所措。我正在调用一个简单的函数来打开与SQL Server 2000数据库的连接并执行一个插入语句。

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql =" INSERT INTO tblStipen(EmployeeID,StipenTypeID,Units,
Amount,AddHours,PayrollID)" ;;
sql + =" VALUES(" + _employeeid +",3,1," + amount + ",0," +
payrollid +")" ;;

尝试
{
cn = new
System.Data.SqlClient .SqlConnection(PayrollSystem.Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery ();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
} 被抽出3次。所以我在数据库中有相同的记录3次??
而且我知道代码只被激活了一次。在传入相同参数的情况下,该功能不会被调用3次。我也没有做任何特殊的线程。

如果我不停地运行程序在这个函数中代码表现为
就像它应该的那样,记录只插入一次。

有什么我想念的吗?还有其他人遇到过这个问题吗?

提前致谢,

Mike R



Mike:


你怎么知道它被执行了3次?如果这不是一个循环并且

没有多个线程运行它,那么我就是那个不是它的房子。
真正执行三个时间。


顺便说一句,我真的会失去动态的sql并使用参数或

理想情况下使它成为一个过程...


sql =" INSERT INTO tblStipen(EmployeeId,StipenTypeID,Units,Amount,

AddHours,PayrollID)值

@EmployeeId, @StipType,@ Units,@ Amount,@ Hours,@ PayrollID)" ;;


cmd.Parameters.Clear();

cmd.Parameters。添加(@ EmployeeId,SqlDbType.Employee'的类型,即Int,

长度).Value = _employeeid;

cmd.Parameters.Add(" @ StipType" ;,SqlDbType.SmallInt).Value = 3;





我猜你说它什么时候开始执行3次它是基于

返回值为3?首先通过查询数据库验证它实际上没有插入3

次。由于它是一个简单的插入,我无法看到3

记录是如何被插入的,但是你可能有一个触发器或什么东西

这是'导致这个数字表现得很有趣。


首先让我知道它是否是数字3作为返回值'是问题还是

如果有可验证的3条记录被插入。


HTH,


Bill

" Mike" <先生******** @ NOSPAMATALLcalibrus.com>在留言中写道

news:uH ************** @ TK2MSFTNGP11.phx.gbl ...

我知道这听起来很奇怪但我不知所措。我正在调用一个简单的函数来打开与SQL Server 2000数据库的连接并执行一个插入语句。

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql =" INSERT INTO tblStipen(EmployeeID,StipenTypeID,Units,
Amount,AddHours,PayrollID)" ;;
sql + =" VALUES(" + _employeeid +",3,1," + amount + ",0," +
payrollid +")" ;;

尝试
{
cn = new
System.Data.SqlClient .SqlConnection(PayrollSystem.Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery ();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
} 被抽出3次。所以我在数据库中有相同的记录3次??
而且我知道代码只被激活了一次。在传入相同参数的情况下,该功能不会被调用3次。我也没有做任何特殊的线程。

如果我不停地运行程序在这个函数中代码表现为
就像它应该的那样,记录只插入一次。

有什么我想念的吗?还有其他人遇到过这个问题吗?

提前致谢,

Mike R



好吧,我可以判断Insert语句被执行了3次

是因为数据库中有3条记录。


1是由ExecuteNonQuery命令返回。没有任何

触发器正在运行。

Mike


William Ryan eMVP <做******** @ comcast.nospam.net>在消息中写道

news:eq ************** @ TK2MSFTNGP10.phx.gbl ...

Mike:

你怎么知道它被执行了3次?如果这不是一个循环并且没有多个线程运行它,那么我将是那个没有真正执行三次的
的房子。
理想地使它成为一个过程...

sql =" INSERT INTO tblStipen(EmployeeId, StipenTypeID,Units,Amount,
AddHours,PayrollID)值
@ EmployeeId,@ StipType,@ Units,@ Amount,@ People,@ PayladID)" ;;

cmd。 Parameters.Clear();
cmd.Parameters.Add(" @ EmployeeId",SqlDbType.Employee''sType ie Int,
Length).Value = _employeeid;
cmd.Parameters。添加(" @ StipType",SqlDbType.SmallInt).Value = 3;



我猜你什么时候说它正在执行3它是基于
返回值为3?首先通过查询数据库验证它实际上没有被插入
3次。由于它是一个简单的插入,我无法看到3
记录是如何被插入的,但是你可能有一个触发器或什么东西
这导致这个数字表现得很有趣。 br />
首先让我知道,如果它是3号作为返回值,那就是问题
或者是否有可验证的3条记录被插入。
HTH,

比尔
迈克 <先生******** @ NOSPAMATALLcalibrus.com>在消息中写道
新闻:uH ************** @ TK2MSFTNGP11.phx.gbl ...

我知道这听起来很奇怪,但我在亏损。我正在调用一个简单的函数来打开与SQL Server 2000数据库的连接,
执行一个Insert语句。

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql =" INSERT INTO tblStipen(EmployeeID,StipenTypeID,Units,
Amount,AddHours,PayrollID)" ;;
sql + =" VALUES(" + _employeeid +",3,1," + amount + ",0," +


payrollid

+")" ;;

尝试
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem.Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn) );
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex ;
}
最后
{
cm .Dispose();
cn.Dispose();
}


当我在调试器中运行此代码并逐步执行此功能时,当
我执行行'int flag = cm.ExecuteNonQuery();"插入
语句被激活3次。所以我在数据库中有相同的记录3
次?而且我知道代码只被激活了一次。函数
没有被调用3次,传入相同的参数。我是
也没有做任何特殊的线程。

如果我运行程序而不停止在这个函数中,代码行为


就像

它应该只记录一次。

有什么我想念的吗?还有其他人遇到过这个问题吗?

提前致谢,

Mike R




I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and executes
an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " + payrollid
+ ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I
execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement
gets exectued 3 times. So I have the same record in the database 3 times??
And I know the the code is only being exectuted once. The function isn''t
being called 3 times with the same parameters being passed in. I am also
not doing any special threading.

If I run the program without stopping in this function the code behave like
it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this problem?

Thanks in advance,

Mike R

解决方案

I have found similar behavior when stepping through code that deals with a
datareader''s read method. What I found was that anytime the debugger was
asked to give me ANY information about the data (i.e. watch, autos and local
windows of the debugger) it would cause the datareader to invoke its read
method because it had to try to get some data for me to look at while
debugging.

It sounds like the ExecuteNonQuery method of the command object acts the
same way in the debugger. This behavior is troubling because it basically
means that you can''t debug (with any degree of certainty) certain
statements.
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...

I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and executes
an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " + payrollid + ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I
execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement
gets exectued 3 times. So I have the same record in the database 3 times??
And I know the the code is only being exectuted once. The function isn''t
being called 3 times with the same parameters being passed in. I am also
not doing any special threading.

If I run the program without stopping in this function the code behave like it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this problem?

Thanks in advance,

Mike R



Mike:

How do you know it''s being executed 3 times? If this isn''t in a loop and
there''s not multiple threads running it, I would be the house that it''s not
really executing three times.

As an aside, I''d really lose the dynamic sql and either use Parameters or
ideally make it a proc...

sql = "INSERT INTO tblStipen(EmployeeId, StipenTypeID, Units, Amount,
AddHours, PayrollID) Values
@EmployeeId, @StipType, @Units, @Amount, @Hours, @PayrollID)";

cmd.Parameters.Clear();
cmd.Parameters.Add("@EmployeeId", SqlDbType.Employee''sType ie Int,
Length).Value = _employeeid;
cmd.Parameters.Add("@StipType", SqlDbType.SmallInt).Value = 3;

etc.

I''m guessing when you say it''s gettting executed 3 times it''s based on the
return value being 3? First verify that it''s not actually being inserted 3
times by querying the DB. Since it''s a simple insert, I can''t see how 3
records could be getting inserted, but you may have a trigger or something
that''s causing this number to behave funny.

First let me know if it''s the number 3 as a return value that''s the issue or
if there are verifiably 3 records getting inserted.

HTH,

Bill
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...

I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and executes
an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " + payrollid + ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I
execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement
gets exectued 3 times. So I have the same record in the database 3 times??
And I know the the code is only being exectuted once. The function isn''t
being called 3 times with the same parameters being passed in. I am also
not doing any special threading.

If I run the program without stopping in this function the code behave like it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this problem?

Thanks in advance,

Mike R



Well the way I can tell that the Insert statement is being executed 3 times
is because there are 3 records in the database.

1 is being returned by the ExecuteNonQuery command. No there aren''t any
triggers running.
Mike

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...

Mike:

How do you know it''s being executed 3 times? If this isn''t in a loop and
there''s not multiple threads running it, I would be the house that it''s not really executing three times.

As an aside, I''d really lose the dynamic sql and either use Parameters or
ideally make it a proc...

sql = "INSERT INTO tblStipen(EmployeeId, StipenTypeID, Units, Amount,
AddHours, PayrollID) Values
@EmployeeId, @StipType, @Units, @Amount, @Hours, @PayrollID)";

cmd.Parameters.Clear();
cmd.Parameters.Add("@EmployeeId", SqlDbType.Employee''sType ie Int,
Length).Value = _employeeid;
cmd.Parameters.Add("@StipType", SqlDbType.SmallInt).Value = 3;

etc.

I''m guessing when you say it''s gettting executed 3 times it''s based on the
return value being 3? First verify that it''s not actually being inserted 3 times by querying the DB. Since it''s a simple insert, I can''t see how 3
records could be getting inserted, but you may have a trigger or something
that''s causing this number to behave funny.

First let me know if it''s the number 3 as a return value that''s the issue or if there are verifiably 3 records getting inserted.

HTH,

Bill
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...

I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " +


payrollid

+ ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement gets exectued 3 times. So I have the same record in the database 3 times?? And I know the the code is only being exectuted once. The function isn''t being called 3 times with the same parameters being passed in. I am also not doing any special threading.

If I run the program without stopping in this function the code behave


like

it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this problem?

Thanks in advance,

Mike R




这篇关于?不知所措? ExecuteNonQuery在调试模式下执行我的Insert语句3次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆