C# 查询执行时不会发生任何事情
我有一个简单的代码C#和SQL Server数据库:C# 查询执行时不会发生任何事情,c#,sql-server,C#,Sql Server,我有一个简单的代码C#和SQL Server数据库: int refcodenum = getOrderNum(); string refcode = "E" + refcodenum; byte[] personalpic = getBarcode(refcodenum); SqlCommand cm2 = new SqlCommand(); cm2.Connection = cn; cm2.CommandText = "Update Clients set ReferenceNumber=
int refcodenum = getOrderNum();
string refcode = "E" + refcodenum;
byte[] personalpic = getBarcode(refcodenum);
SqlCommand cm2 = new SqlCommand();
cm2.Connection = cn;
cm2.CommandText = "Update Clients set ReferenceNumber='" + refcode + "',ReferenceBarcode=@photo where NetNumber='"+id+"'";
cm2.Parameters.Add("@photo", SqlDbType.Image, personalpic.Length).Value = personalpic;
// here like cursor stop
cm2.ExecuteNonQuery();
lastpage = "x";
File.Delete(Directory.GetCurrentDirectory() + @"\myimage.jpg");
我已经运行了它,但是在执行查询时没有发生任何事情,我使用了MessageBox
这样的方法来识别出现问题的行
int refcodenum = getOrderNum();
string refcode = "E" + refcodenum;
byte[] personalpic = getBarcode(refcodenum);
SqlCommand cm2 = new SqlCommand();
cm2.Connection = cn;
MessageBox.Show("1");
cm2.CommandText = "Update Clients set ReferenceNumber='" + refcode + "',ReferenceBarcode=@photo where NetNumber='"+id+"'";
MessageBox.Show("2");
cm2.Parameters.Add("@photo", SqlDbType.Image, personalpic.Length).Value = personalpic;
MessageBox.Show("3");
// here cursor stops
cm2.ExecuteNonQuery();
// that messagebox isn't shown
MessageBox.Show("4");
lastpage = "x";
File.Delete(Directory.GetCurrentDirectory() + @"\myimage.jpg");
如果您尚未打开连接,将不胜感激。见下文 您还应该使用
using
语法来使用IDisposable
int refcodenum = getOrderNum();
string refcode = "E" + refcodenum;
byte[] personalpic = getBarcode(refcodenum);
var sqlCmdText = "Update Clients set ReferenceNumber='" + refcode + "',ReferenceBarcode=@photo where NetNumber='"+id+"'";
try
{
using (var sqlConnection = new SqlConnection([YOUR CONNECTION STRING HERE]))
{
using (var sqlCommand = new SqlCommand(sqlCmdText, sqlConnection))
{
sqlCommand.CommandType = CommandType.Text;
sqlCommand.Parameters.Add("@photo", SqlDbType.Image, personalpic.Length).Value = personalpic;
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw new DataException(ex.Message);
}
“光标停止”?你是说调试器停在那一行吗?这里有断点吗?我的意思是,就像解释器在这里停止一样,根本没有任何事情发生。C#没有被解释,它是调试器。你确定左边没有断点-红色圆圈吗?没有显示错误?也许你可以上传一个屏幕截图。我删除了整个条件语句(其中…“+id+”),但什么都没有发生。你的输出窗口中有什么?