VB.NET中怎麼用代码实现DataGridview的数据绑定?
现在我要用代码实现VB.net中DataGridview的数据绑定,数据库名为UserInfo.mbd,裏面的字段有UerID,UserName等字段,哪位大侠能给个完整...
现在我要用代码实现VB.net中DataGridview的数据绑定,数据库名为UserInfo.mbd,裏面的字段有UerID,UserName等字段,哪位大侠能给个完整点的代码,带数据连接的,有增删改的代码最好
展开
2个回答
展开全部
一般来说:连接数据库这一块都做成DLL,这样方便
在配置文件app.config中加入
<appSettings>
<add key="LotDBConnection" value="DATA SOURCE=数据库连接;PASSWORD=密码;PERSIST SECURITY INFO=True;USER ID=登录名"/>
</appSettings>
//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中加入
<appSettings>
<add key="LotDBConnection" value="DATA SOURCE=数据库连接;PASSWORD=密码;PERSIST SECURITY INFO=True;USER ID=登录名"/>
</appSettings>
//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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询