过去三个小时。我在网上搜索了我的问题的解决方案。我找到了一些,但没有一个工作(!?)。错误发生在btnAdd Sub中。我无法使用这种形式的声明:'DECLARE @cds SqlDBType.SmallInt因为我会收到以下错误:Statement在方法/多行lambda中无效。我也使用了他们的声明形式http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.insertcommand%28v=vs.110%29.aspx但又一次......我收到同样的错误。评论的部分是我尝试过的一切。也许你可以帮我解决这个问题。 :)
更新了正确的解决方案:
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
con.Open()
Dim dr As DataRow
mycmd.CommandText = "insert into studenti(cods, nrmatricol, nume, grupa, datan) values(@cs,@nm,@n,@g,@dn)"
Dim p1 As New SqlParameter
Dim p2 As New SqlParameter
Dim p3 As New SqlParameter
Dim p4 As New SqlParameter
Dim p5 As New SqlParameter
mycmd.Parameters.Clear()
p1.ParameterName = "@cs"
p1.Value = Convert.ToInt16(txtCodS.Text)
mycmd.Parameters.Add(p1)
p2.ParameterName = "@nm"
p2.Value = txtNrMat.Text
mycmd.Parameters.Add(p2)
p3.ParameterName = "@n"
p3.Value = txtNume.Text
mycmd.Parameters.Add(p3)
p4.ParameterName = "@g"
p4.Value = txtGrupa.Text
mycmd.Parameters.Add(p4)
p5.ParameterName = "@dn"
p5.Value = Convert.ToDateTime(txtDataN.Text)
mycmd.Parameters.Add(p5)
mycmd.Connection = con
da1.InsertCommand = mycmd
dr = DataSet1.Tables("studenti").NewRow()
dr("cods") = Convert.ToInt16(txtCodS.Text)
dr("nrmatricol") = txtNrMat.Text
dr("nume") = txtNume.Text
dr("grupa") = txtGrupa.Text
dr("datan") = Convert.ToDateTime(txtDataN.Text)
DataSet1.Tables("studenti").Rows.Add(dr)
da1.Update(DataSet1, "studenti")
UpdateUI()
con.Close()
End Sub
错:
Dim da1 As New SqlClient.SqlDataAdapter
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
con.Open()
Dim dr As DataRow
Dim mycmd As New SqlClient.SqlCommand
//Dim mycmd2 As New SqlClient.SqlCommand
//mycmd2.Connection = con
// mycmd2.CommandText = "select* from studenti"
//Dim v(4) As Object
//v(0) = Convert.ToInt16(txtCodS.Text)
//v(1) = txtNrMat.Text
//v(2) = txtNume.Text
//v(3) = txtGrupa.Text
// v(4) = txtDataN.Text
mycmd.CommandText = "insert into studenti (cods,nrmatricol,nume,grupa,datan) values (@cds,@nm,@n,@g,@dn)"
Dim p1 As New SqlClient.SqlParameter
p1.ParameterName = "@cds"
p1.Value = Convert.ToInt16(txtCodS.Text)
//DECLARE @cds SqlDBType.SmallInt
//cmd.Parameters.Clear()
cmd.Parameters.Add(p1)
//cmd.Parameters.Add("@cds", SqlDbType.SmallInt).Value = Convert.ToInt16(txtCodS.Text)
cmd.Parameters.Add("@nm", SqlDbType.VarChar, 10)
cmd.Parameters.Add("@n", SqlDbType.VarChar, 30)
cmd.Parameters.Add("@g", SqlDbType.VarChar, 10)
cmd.Parameters.Add("@dn", SqlDbType.DateTime)
mycmd.Connection = con
da1.InsertCommand = mycmd
dr = DataSet1.Tables("studenti").NewRow()
dr("cods") = Convert.ToInt16(txtCodS.Text)
dr("nrmatricol") = txtNrMat.Text
dr("nume") = txtNume.Text
dr("grupa") = txtGrupa.Text
dr("datan") = Convert.ToDateTime(txtDataN.Text)
DataSet1.Tables("studenti").Rows.Add(dr)
da1.Update(DataSet1, "studenti")
UpdateUI()
con.Close()
End Sub
End Class
答案 0 :(得分:0)
@cds变量在被cmd.Parameters.Clear()
删除之后设置。
只需删除此行代码并为@cds提供值。