C#-SQLCommand-转换varchar值时转换失败';System.Windows.Forms.Label,文本:69470570';到数据类型int
我是编程新手,这里需要一些帮助 在internet上搜索,但找不到解决方案 我有一些小代码,使用winfrom应用程序和c# 这是问题部分的一部分:C#-SQLCommand-转换varchar值时转换失败';System.Windows.Forms.Label,文本:69470570';到数据类型int,c#,sql,C#,Sql,我是编程新手,这里需要一些帮助 在internet上搜索,但找不到解决方案 我有一些小代码,使用winfrom应用程序和c# 这是问题部分的一部分: myConnection.Open(); sqlCommand myCommand16 = new SqlCommand("SELECT Id FROM [dbo].[Units] where unitid='" + textBox1.Text + "'", myConnection); internalUnitId =
myConnection.Open();
sqlCommand myCommand16 = new SqlCommand("SELECT Id FROM [dbo].[Units] where unitid='" + textBox1.Text + "'", myConnection);
internalUnitId = myCommand16.ExecuteScalar().ToString();
SqlCommand myCommand15 = new SqlCommand("SELECT TOP 1 REQUESTID FROM [dbo].[ActivationProcStatusMsgd] where unitsid='" + internalUnitId + "' order by id desc", myConnection);
label11.Text = myCommand15.ExecuteScalar().ToString();
// till here all ok.
SqlCommand myCommand25 = new SqlCommand("SELECT TOP 1 [OriginalReqId] FROM [dbo].[ActivationRequestsHistory] where unitsid='" + internalUnitId + "' and originalreqid='" + label11 + "' order by id desc", myConnection);
label18.Text = myCommand25.ExecuteScalar().ToString();
//in This part above i'm getting the exception
在下面的例子中,我用69470570替换“label11”,一切正常。不理解不同的
// SqlCommand myCommand25 = new SqlCommand("SELECT TOP 1 [OriginalReqId] FROM [dbo].[ActivationRequestsHistory] where unitsid='" + internalUnitId + "' and originalreqid='69470570' order by id desc", myConnection);
label18.Text = myCommand25.ExecuteScalar().ToString();
提前谢谢
Ohad而不是查询中的label11写入label11。Text您没有添加。label11附近的文本:
SqlCommand myCommand25 = new SqlCommand("SELECT TOP 1 [OriginalReqId] FROM [dbo].[ActivationRequestsHistory] where unitsid='" + internalUnitId + "' and originalreqid='" + label11.Text + "' order by id desc", myConnection);
问题如下:
SqlCommand myCommand25 = new SqlCommand("SELECT TOP 1 [OriginalReqId] FROM [dbo].[ActivationRequestsHistory] where unitsid='" + internalUnitId + "' and originalreqid='" + label11 + "' order by id desc", myConnection);
这里有如下查询结构:
originalreqid='" + label11 + "'
应该是:
originalreqid='" + label11.Text + "'
完整线路为:
SqlCommand myCommand25 = new SqlCommand("SELECT TOP 1 [OriginalReqId] FROM [dbo].[ActivationRequestsHistory] where unitsid='" + internalUnitId + "' and originalreqid='" + label11.Text + "' order by id desc", myConnection);
原因:
label11
只是您正在使用的对象。您必须提到它的属性.Text
,以便获得正确的值。您应该使用label11.Text。label11是标签的名称id。它只是一个对象,无法转换为int。对不起,我不太了解您的任务,但这里有一些建议供您参考:1。尝试更好地命名控件,不要使用label1和label2。尝试使用参数化SQL Wow。。太快了:)。刚刚试过,效果不错-谢谢大家,谢谢。