555~~~穷死了,最后40分了,希望大家帮我看看,谢谢!

philins 2003-03-22 05:30:21
我要实现在DataGrid中对数据库的更新,数据源是sql server中的student表,主键是Sno,现在的问题是,不能修改学号!真的是很郁闷!我看了半天代码也没有发现错误,大家帮我看看吧!谢谢了,最后的40分全给大家了,呵呵(明天就不能来了,穷死了)
update函数代码如下:
sub Stu_Update(sender as object,E as DataGridCommandEventArgs)
dim UComm as SqlCommand
dim UpdateCmd as string
dim EditText1,EditText2,EditText3 as textbox
UpdateCmd="update student set Sno=@Sno,Sname=@Sname,Sbirth=@Sbirth Where Sno=@Sno"
UComm=new SQLCommand(UpdateCmd,objConnection)

UComm.Parameters.Add(New SqlParameter("@Sno", SqlDbType.VarChar))
UComm.Parameters.Add(New SqlParameter("@Sname", SqlDbType.VarChar))
UComm.Parameters.Add(New SqlParameter("@Sbirth", SqlDbType.VarChar))

UComm.Parameters("@Sno").Value = _
StuData.DataKeys(CInt(e.Item.ItemIndex))

Dim cols() As String = {"@Sno","@Sname","@Sbirth"}
Dim numCols As Integer = E.Item.Cells.Count
'Dim i As Integer
Dim colvalue As String
Dim txtBox As Textbox

txtBox =E.Item.FindControl("txtSno")
colvalue = txtBox.Text
If (colvalue = "")
ErrorMessage.text = "ERROR: Null values not allowed for 学号"
ErrorMessage.Style("color") = "red"
Exit Sub
End If
UComm.Parameters(cols(0)).Value = colvalue

txtBox = E.Item.FindControl("txtSname")
colvalue = txtBox.Text
If (colvalue = "")
ErrorMessage.text = "ERROR: Null values not allowed for 姓名"
ErrorMessage.Style("color") = "red"
Exit Sub
End If
UComm.Parameters(cols(1)).Value = colvalue

txtBox = E.Item.FindControl("txtSbirth")
colvalue = txtBox.Text
If (colvalue = "")
ErrorMessage.text = "ERROR: Null values not allowed for 出生年月"
ErrorMessage.Style("color") = "red"
Exit Sub
End If
UComm.Parameters(cols(2)).Value = colvalue

UComm.Connection.Open()

try
UComm.ExecuteNonQuery()

Session("MyData")=nothing

StuData.EditItemIndex=-1

Catch exp as SQLexception
if exp.number=2627 then
ErrorMessage.text="已存在同样的记录"
ErrorMessage.Style("Color")="red"
Else
ErrorMessage.Text="不能更新记录"
End if
end try

'关闭连接
UComm.Connection.Close()
BindGrid()
ErrorMessage.text=""
end sub
...全文
24 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
philins 2003-03-22
  • 打赏
  • 举报
回复
哦,谢谢!结帐喽!
ljfppp 2003-03-22
  • 打赏
  • 举报
回复
custDA is a SqlDataAdapter
philins 2003-03-22
  • 打赏
  • 举报
回复
lbx1979(Love Arsenal):
custDA?What's that?a SQLCommand?

Thank all of you VERY MUCH!
I'll pay the points now!
saucer 2003-03-22
  • 打赏
  • 举报
回复
sorry, didn't see lbx1979(Love Arsenal)'s post, his solution is better
lbx1979 2003-03-22
  • 打赏
  • 举报
回复
The following code example defines an UPDATE statement in which the CustomerID column is used as a SourceColumn for two parameters: @CustomerID (SET CustomerID = @CustomerID), and @OldCustomerID (WHERE CustomerID = @OldCustomerID). The @CustomerID parameter is used to update the CustomerID column to the current value in the DataRow. As a result, the CustomerID SourceColumn with a SourceVersion of Current is used. The @OldCustomerID parameter is used to identify the current row in the data source. Because the matching column value is found in the Original version of the row, the same SourceColumn (CustomerID) with a SourceVersion of Original is used.

string updateSQL = "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName WHERE CustomerID = @OldCustomerID";

custDA.UpdateCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");

custDA.UpdateCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

SqlParameter myParm = custDA.UpdateCommand.Parameters.Add("@OldCustomerID", SqlDbType.NChar, 5, "CustomerID");
myParm.SourceVersion = DataRowVersion.Original;

MSDN上的例子
saucer 2003-03-22
  • 打赏
  • 举报
回复
UpdateCmd="update student set Sno=@Sno,Sname=@Sname,Sbirth=@Sbirth Where Sno=@OldSno"
UComm=new SQLCommand(UpdateCmd,objConnection)

UComm.Parameters.Add(New SqlParameter("@Sno", SqlDbType.VarChar))
UComm.Parameters.Add(New SqlParameter("@Sname", SqlDbType.VarChar))
UComm.Parameters.Add(New SqlParameter("@Sbirth", SqlDbType.VarChar))
UComm.Parameters.Add(New SqlParameter("@OldSno", SqlDbType.VarChar))


....
UComm.Parameters("@OldSno").Value = _
StuData.DataKeys(CInt(e.Item.ItemIndex))
....

lbx1979 2003-03-22
  • 打赏
  • 举报
回复
Sno也可以修改,不过要定义两个Parameters对应Sno,分别设置SourceVersion:
update student set Sno=@newSno,Sname=@Sname,Sbirth=@Sbirth Where Sno=@oraSno
放在where子句里的parameter要设置SourceVersion = DataRowVersion.Original
要更新的那个值就用默认的就可以了
philins 2003-03-22
  • 打赏
  • 举报
回复
哦,好像是有问题,不过我如果想要同时修改Sno呢?
saucer 2003-03-22
  • 打赏
  • 举报
回复
change

update student set Sno=@Sno,Sname=@Sname,Sbirth=@Sbirth Where Sno=@Sno
===>
update student set Sname=@Sname,Sbirth=@Sbirth Where Sno=@Sno
j99616 2003-03-22
  • 打赏
  • 举报
回复
UpdateCmd="update student set Sno=@Sno,Sname=@Sname,Sbirth=@Sbirth Where Sno=@Sno"

是不是有问题

62,249

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧