你能用数据表帮我吗? [英] Could you help me with datatable

查看:60
本文介绍了你能用数据表帮我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过循环从sql server向数据表添加行,但是它给了我这条消息在第1位没有行。

它只添加第一项然后抛出例外。



我尝试了什么:



Im trying to add rows to the datatable by loop from sql server , but it gives me this message "There is no row at position 1 ".
it add only the first item then throw the exception.

What I have tried:

For i As Integer = 0 To getCount() - 1
    If InShift(Callsql.SqlDset.Tables(0).Rows(i)("e_ID")) = False Then
           Callsql2.RunQuery2("SELECT Job_Name FROM Jobs where Jo_ID ='" & Callsql.SqlDset.Tables(0).Rows(i)("Job_ID") & "' ")
           table2.Rows.Add(Callsql.SqlDset.Tables(0).Rows(i)("e_ID"), Callsql.SqlDset.Tables(0).Rows(i)("PC_ID"), Callsql.SqlDset.Tables(0).Rows(i)("e_Name").ToString(), Callsql.SqlDset.Tables(0).Rows(i)("e_LaName").ToString(), Callsql2.SqlDset.Tables(0).Rows(i)("Job_Name").ToString())
            DataGridView4.DataSource = table2
    End If
Next

推荐答案

您遇到麻烦的原因在于:

The reason of your trouble is here:
DataGridView4.DataSource = table2



如命令重载 DataSource for DataGridView4 。如果你在中使用它来进行...下一个循环,那么只有一个项目已被选中并加载了这些行:


Such as command reload DataSource for DataGridView4. If you use it inside for ...next loop, there's only one item, which has been selected and loaded with those lines:

Callsql2.RunQuery2("SELECT Job_Name FROM Jobs where Jo_ID ='" & Callsql.SqlDset.Tables(0).Rows(i)("Job_ID") & "' ")
table2.Rows.Add(...)



移动行,用于重新加载数据,在循环外部,一切都应该没问题。



BTW:永远不要让你的数据库进行循环!它会杀死Sql Server的性能,这是麻烦的常见原因!


Move line, which is used to reload data, outside the loop and everything should be OK.

BTW: Never ask your database in for loop! It kills Sql Server performance and it's a common reason of troubles!


感谢所有人的帮助..

我发现erorr在哪里:



thank all for help ..
I find out where was the erorr :

Callsql2.RunQuery2("SELECT Job_Name FROM Jobs where Jo_ID ='" & Callsql.SqlDset.Tables(0).Rows(i)("Job_ID") & "' ")





我修好了



and I fix it


这篇关于你能用数据表帮我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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