我写了如下代码来操作数据库,可是什么功能也实现不了,各位英雄豪杰帮忙看看,解决了就有分
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
div1.Visible = False
div2.Visible = False btnFirst.Text = "最首页"
btnPrev.Text = "前一页"
btnNext.Text = "下一页"
btnLast.Text = "最后页"
myconn()
BindGrid()
End Sub
Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
Dim startIndex As Integer
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindGrid()
ShowStats()
End Sub Sub BindGrid()
myconn()
Dim ds As DataSet = New DataSet
Dim adapter As SqlDataAdapter = New SqlDataAdapter("Select * from student", myConnection)
adapter.Fill(ds, "student")
MyDataGrid.DataSource = ds.Tables("student").DefaultView
MyDataGrid.DataBind()
ShowStats() End Sub Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
'used by external paging UI
Dim arg As String = sender.CommandArgument Select Case arg
Case "next"
If (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) Then
MyDataGrid.CurrentPageIndex += 1
End If
Case "prev"
If (MyDataGrid.CurrentPageIndex > 0) Then
MyDataGrid.CurrentPageIndex -= 1
End If
Case "last"
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1)
Case Else
'page number
MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg)
End Select
BindGrid()
ShowStats()
End Sub Sub ShowStats()
lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页"
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页" End Sub
Public myconnection As SqlConnection
Public myconnectionstring As String
Public conver As Integer
Function myconn() '链接数据库
myconnectionstring = "server=(LOCAL);database=test;user ID=sa;password=1234"
myconnection = New SqlConnection(myconnectionstring) End Function Public Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click '添加记录
div2.Visible = True
conver = 1
End Sub Public Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click '删除记录
conver = 1
div1.Visible = True
labinput.Text = "请输入你要删除的ID号:"
End Sub Public Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click '修改记录
div2.Visible = True
conver = 2 End Sub Public Sub button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button4.Click '查询记录
myconn()
div1.Visible = True
labinput.Text = "请输入您要查询的ID号:" End Sub
Public Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click '确定修改和添加事件
Select Case (conver)
Case 1
myconn()
Dim insstr As String = "Insert Into student (ID,学号,姓名,性别,成绩)values(" + userId.Text.ToString + "," + usernum.Text.ToString + "," + name.Text + "," + sex.Text + "," + grade.Text.ToString + ")"
condata(insstr, 1)
condata("select * from student order by ID", 0)
Case 2
myconn()
Dim upstr As String = "UPDATA student SET 学号 = " + usernum.Text.ToString + ",姓名=" + name.Text + " , 性别= " + sex.Text + " ,成绩=" + grade.Text.ToString + "where ID = " + userId.Text.ToString
condata(upstr, 1)
condata("select * from student order by ID", 0)
End Select
End Sub
Function condata(ByVal constr As String, ByVal show As Integer) As Boolean '操作数据库事件
Try Dim mycommand As SqlCommand = New SqlCommand(constr, myconnection)
myconnection.Open()
Dim mydatareader As SqlDataReader = mycommand.ExecuteReader()
Dim ds As DataSet = New DataSet If show = 0 Then
MyDataGrid.DataSource = mydatareader
MyDataGrid.DataBind()
End If
myconnection.Close()
Catch ee As Exception
myconnection.Close()
info.Text = "发生错误:" + ee.Message + "或者,请您确认输入的数据库名称无误:" Finally
End Try
End Function Public Sub button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button5.Click '确定删除和查询事件
Select Case (conver)
Case 1
myconn()
condata("delete * from student where ID=" + input1.Value, 1)
condata("Select * from student Order by ID", 0)
Case 2
myconn()
condata("select * from student where ID=" + input1.Value, 0) End Select
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
div1.Visible = False
div2.Visible = False btnFirst.Text = "最首页"
btnPrev.Text = "前一页"
btnNext.Text = "下一页"
btnLast.Text = "最后页"
myconn()
BindGrid()
End Sub
Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
Dim startIndex As Integer
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindGrid()
ShowStats()
End Sub Sub BindGrid()
myconn()
Dim ds As DataSet = New DataSet
Dim adapter As SqlDataAdapter = New SqlDataAdapter("Select * from student", myConnection)
adapter.Fill(ds, "student")
MyDataGrid.DataSource = ds.Tables("student").DefaultView
MyDataGrid.DataBind()
ShowStats() End Sub Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
'used by external paging UI
Dim arg As String = sender.CommandArgument Select Case arg
Case "next"
If (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) Then
MyDataGrid.CurrentPageIndex += 1
End If
Case "prev"
If (MyDataGrid.CurrentPageIndex > 0) Then
MyDataGrid.CurrentPageIndex -= 1
End If
Case "last"
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1)
Case Else
'page number
MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg)
End Select
BindGrid()
ShowStats()
End Sub Sub ShowStats()
lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页"
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页" End Sub
Public myconnection As SqlConnection
Public myconnectionstring As String
Public conver As Integer
Function myconn() '链接数据库
myconnectionstring = "server=(LOCAL);database=test;user ID=sa;password=1234"
myconnection = New SqlConnection(myconnectionstring) End Function Public Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click '添加记录
div2.Visible = True
conver = 1
End Sub Public Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click '删除记录
conver = 1
div1.Visible = True
labinput.Text = "请输入你要删除的ID号:"
End Sub Public Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click '修改记录
div2.Visible = True
conver = 2 End Sub Public Sub button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button4.Click '查询记录
myconn()
div1.Visible = True
labinput.Text = "请输入您要查询的ID号:" End Sub
Public Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click '确定修改和添加事件
Select Case (conver)
Case 1
myconn()
Dim insstr As String = "Insert Into student (ID,学号,姓名,性别,成绩)values(" + userId.Text.ToString + "," + usernum.Text.ToString + "," + name.Text + "," + sex.Text + "," + grade.Text.ToString + ")"
condata(insstr, 1)
condata("select * from student order by ID", 0)
Case 2
myconn()
Dim upstr As String = "UPDATA student SET 学号 = " + usernum.Text.ToString + ",姓名=" + name.Text + " , 性别= " + sex.Text + " ,成绩=" + grade.Text.ToString + "where ID = " + userId.Text.ToString
condata(upstr, 1)
condata("select * from student order by ID", 0)
End Select
End Sub
Function condata(ByVal constr As String, ByVal show As Integer) As Boolean '操作数据库事件
Try Dim mycommand As SqlCommand = New SqlCommand(constr, myconnection)
myconnection.Open()
Dim mydatareader As SqlDataReader = mycommand.ExecuteReader()
Dim ds As DataSet = New DataSet If show = 0 Then
MyDataGrid.DataSource = mydatareader
MyDataGrid.DataBind()
End If
myconnection.Close()
Catch ee As Exception
myconnection.Close()
info.Text = "发生错误:" + ee.Message + "或者,请您确认输入的数据库名称无误:" Finally
End Try
End Function Public Sub button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button5.Click '确定删除和查询事件
Select Case (conver)
Case 1
myconn()
condata("delete * from student where ID=" + input1.Value, 1)
condata("Select * from student Order by ID", 0)
Case 2
myconn()
condata("select * from student where ID=" + input1.Value, 0) End Select
End Sub
解决方案 »
- 请教一个比较复杂的问题(最要是要求效率),高手请进
- 哪位大哥提供一些网页布局、浏览器兼容方面的材料呢?
- SQL返回值。。。
- asp.net 2.0中CSS定位用absolute不能准确定位?
- DetailView绑定ObjectSource的问题,只有可见的数据项才会被更新到对象里??
- 用 c#进行服务器端编程,请问如何保护代码,能否把代码做成.dll
- 还是关于showModalDialog的刷新的问题,请朋友帮忙,谢谢,急呀!!!
- SQL 问题。。。(在线等。。。)。
- 如何把打好包的.net装在一台新服务器上?
- WebFrom有無辦法只檢索一次到DataSet中﹐DataGrid翻頁時不用再新從后台數據庫中取數據到DataSet
- ************超难问题*********
- DataView能限定显示的行数吗?
你的myconnection怎麽沒有OPEN呢?