将参数值设置为等于 DataColumn 值时出现问题
在 VB.Net 中编码
大家好,我遇到了这个问题,我试图为存储过程分配一些参数,其中一些值存储在数据表中。在代码中,我可以将参数值设置为数据表,并给出列标题或索引,没有问题,但是当它尝试在运行时执行时,它会崩溃。
我得到的错误是“无法将参数值从 DataColumn 转换为字符串”的变体,具体取决于我尝试分配该值的方式。
代码中其他地方的数据类型都是正确的,所以我很确定这不是问题,我想我只是以错误的方式处理这个问题。
我的这个位代码是
Try
ConnectionAttempt.Open()
Try
With NewYearCommand_Dates
.CommandType = CommandType.StoredProcedure
.CommandText = "DatesINSERT"
.Parameters.Add(New SqlParameter("@AcademicYear", SqlDbType.VarChar))
.Parameters.Add(New SqlParameter("@YearStart", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@YearEnd", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term1Start", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term1End", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term2Start", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term2End", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term3Start", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term3End", SqlDbType.Date))
.Parameters("@AcademicYear").Value = dates.Columns("AcademicYear")
.Parameters("@YearStart").Value = dates.Columns("YearStart")
.Parameters("@YearEnd").Value = dates.Columns("YearEnd")
.Parameters("@Term1Start").Value = dates.Columns("Term1Start")
.Parameters("@Term1End").Value = dates.Columns("Term1End")
.Parameters("@Term2Start").Value = dates.Columns("Term2Start")
.Parameters("@Term2End").Value = dates.Columns("Term2End")
.Parameters("@Term3Start").Value = dates.Columns("Term3Start")
.Parameters("@Term3End").Value = dates.Columns("Term3End")
.Connection = ConnectionAttempt
.ExecuteNonQuery()
.Dispose()
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
我试图让它工作的其他事情是 .Value=dates 位的所有变体,例如
.Value=dates(0) (因此使用列索引)
.Value=dates.columns( 0)(再次使用列索引)
我还尝试声明变量,并将其值设置为列中的值,但代码返回一个错误,指出“类型‘System.Data.DataColumn’的值无法转换为细绳”。
我应该指出,我在这个子表中同时对 3 个表执行此操作,其中 2 个表只有一行数据,但第三个表的行数不确定。
恐怕我不知道还能做什么。我的伙伴和讲师引导我相信这应该有效,当然它会有效,但证据表明事实并非如此。
如果我完全以错误的方式处理这件事,有人可以启发我我应该做什么吗?
干杯
Coding in VB.Net
Hi guys I've got this problem where I'm trying to assign some parameters for a stored procedure with some values that are stored in a data table. in the code, I can set the parameter values to the data table, and give a column header or index without issue, but when it tries to execute at runtime it bombs out.
the error I get is a variation on "Failed to Convert Parameter Value from a DataColumn to a String" depending on exactly how I try to assign the value.
the data types are all correct elsewhere in the code, so I'm pretty certain that's not the issue, I guess I'm just going about this the wrong way.
my code for this bit is
Try
ConnectionAttempt.Open()
Try
With NewYearCommand_Dates
.CommandType = CommandType.StoredProcedure
.CommandText = "DatesINSERT"
.Parameters.Add(New SqlParameter("@AcademicYear", SqlDbType.VarChar))
.Parameters.Add(New SqlParameter("@YearStart", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@YearEnd", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term1Start", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term1End", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term2Start", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term2End", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term3Start", SqlDbType.Date))
.Parameters.Add(New SqlParameter("@Term3End", SqlDbType.Date))
.Parameters("@AcademicYear").Value = dates.Columns("AcademicYear")
.Parameters("@YearStart").Value = dates.Columns("YearStart")
.Parameters("@YearEnd").Value = dates.Columns("YearEnd")
.Parameters("@Term1Start").Value = dates.Columns("Term1Start")
.Parameters("@Term1End").Value = dates.Columns("Term1End")
.Parameters("@Term2Start").Value = dates.Columns("Term2Start")
.Parameters("@Term2End").Value = dates.Columns("Term2End")
.Parameters("@Term3Start").Value = dates.Columns("Term3Start")
.Parameters("@Term3End").Value = dates.Columns("Term3End")
.Connection = ConnectionAttempt
.ExecuteNonQuery()
.Dispose()
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
other things I've tried to get this to work are all variations on the .Value=dates bit, such as
.Value=dates(0) (so using column index)
.Value=dates.columns(0) (again using column index)
I've also tried declaring variables, and setting their values to the values in the columns, but the code returns an error saying that "Value of type 'System.Data.DataColumn' cannot be converted to string".
I should point out that i'm doing this for 3 tables at once in this sub, 2 of them will only have a single row of data, but the third has an indeterminate number of rows.
I'm afraid I don't know what else to do. I'm lead to believe by my mate and my lecturer that this should work, and of course it will work, but the evidence says otherwise.
if I'm going about this entirely the wrong way, could someone please enlighten me as to what i SHOULD be doing?
cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这
是:整个列。您只能分配行中的值。
例如,要使用 DataTable 的第一行:
This:
Is a whole column. You can only assign the value from one row.
For example, to use the first row of the DataTable: