2013-03-20 105 views
-1

我有更新语句的问题。这是我的vb.net服务器端代码:vb.net中的更新查询不起作用

Public Function UpdateRow() As DataTable 

    Dim query As String = "Update Intranet.dbo.Gn_ISCoordinators SET cUserName='" & tbUsername.Text.Trim() & 
     "',lDeptUser=" & CByte(rblDept.SelectedIndex) & " WHERE cUserName=" & "'" & Session("Edit") & "'" 
    Dim hehe As DataTable = New DataTable() 
    Using adapter1 = New SqlDataAdapter(query, ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString) 
     adapter1.Fill(hehe) 
     Response.Redirect("~/Home.aspx") 
     Session("Edit") = Nothing 
     Return hehe 
    End Using 

End Function 

而且我在这里称之为:

Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click 
If Not Session("Edit") Is Nothing Then 
     UpdateRow() 
End If 

我想它得到文本从单选按钮列表的数据,并将数据和更新查询与数据库上的一个。在我的情况下,Session("Edit")与我想用文本框的文本替换它的域相同。当我按下按钮时,它不会给我任何改变。问题究竟在哪里?

请帮帮我。

Thanx提前。

回答

1

您正在运行更新查询。您需要使用ExecuteNonQuery执行它,适配器的Fill命令会从数据库中读取数据而不写入它。并且您的数据不在数据库中

Public Function UpdateRow() As DataTable 

    Dim query As String = "Update Intranet.dbo.Gn_ISCoordinators SET [email protected]" + 
          ",[email protected] WHERE [email protected]" 
    Using con = new SqlConnection(ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString)  
     con.Open() 
     Dim cmd = new SqlCommand(query, con) 
     cmd.Parameters.AddWithValue("@uname", tbUsername.Text.Trim()) 
     cmd.Parameters.AddWithValue("@dept", CByte(rblDept.SelectedIndex)) 
     cmd.Parameters.AddWithValue("@oldname", Session("Edit")) 
     cmd.ExecuteNonQuery() 
    End Using 
End Function 

还请注意,您不应串联字符串以形成sql命令。这种方法导致了一个非常严重的安全问题,称为Sql Injection

+0

它给了我一个绿色的行在'结束函数'并写'空引用异常可能会发生'。另外,它不会和我一起工作。 – 7alhashmi 2013-03-20 08:50:49

+0

你有什么错误? (除了绿线) – Steve 2013-03-20 08:57:58

+0

没有错误,而不是绿线。但它不起作用。我不知道为什么。在我按下按钮之后,它只保留用户名。仿佛我什么都没做。 – 7alhashmi 2013-03-20 09:01:01