VB.NET中怎麼用代码实现DataGridview的数据绑定?
1个回答
展开全部
一般来说:连接数据库这一块都做成DLL,这样方便
在配置文件app.config中加入
//m_SqlPath
存放SQL语句的XML文件
Dim
sqlHelper
As
New
SQLHelper("LotDBConnection")
Dim
sqlRead
As
New
ResourceHelper
Dim
ht
As
New
Hashtable
Dim
strSQL
As
String
Try
'设置SQL文参数
ht.Clear()
ht.Add(ColumnName.USER_ID.ToString,
Trim(Me.txtUserID.Text))
ht.Add(ColumnName.USER_NAME.ToString,
Trim(Me.txtUserName.Text))
'数据查询
strSQL
=
sqlRead.GetSQLSentence("GetUserList",
m_SqlPath,
ht)
ds
=
sqlHelper.ExecuteDataSet(strSQL)
If
ds.Tables(0).Rows.Count
=
0
Then
MessageBox
.Show
("数据不存在");
Else
'数据绑定
dgvUserMeisai.DataSource
=
ds.Tables(0)
End
If
Catch
ex
As
Exception
MessageBox
.Show
(ex.Message
);
Finally
sqlHelper
=
Nothing
sqlRead
=
Nothing
ht
=
Nothing
End
Try
在配置文件app.config中加入
//m_SqlPath
存放SQL语句的XML文件
Dim
sqlHelper
As
New
SQLHelper("LotDBConnection")
Dim
sqlRead
As
New
ResourceHelper
Dim
ht
As
New
Hashtable
Dim
strSQL
As
String
Try
'设置SQL文参数
ht.Clear()
ht.Add(ColumnName.USER_ID.ToString,
Trim(Me.txtUserID.Text))
ht.Add(ColumnName.USER_NAME.ToString,
Trim(Me.txtUserName.Text))
'数据查询
strSQL
=
sqlRead.GetSQLSentence("GetUserList",
m_SqlPath,
ht)
ds
=
sqlHelper.ExecuteDataSet(strSQL)
If
ds.Tables(0).Rows.Count
=
0
Then
MessageBox
.Show
("数据不存在");
Else
'数据绑定
dgvUserMeisai.DataSource
=
ds.Tables(0)
End
If
Catch
ex
As
Exception
MessageBox
.Show
(ex.Message
);
Finally
sqlHelper
=
Nothing
sqlRead
=
Nothing
ht
=
Nothing
End
Try
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询