我为什么会收到此错误
表没有主键。
尽管我在创建表(pdfinfo)时将(idpdf)设置为主要
在此行
Dim row As DataRow = dt.Rows.Find(ComboBox1.SelectedValue)
Private Sub mylib_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
adapter = New SqlDataAdapter("select * from pdfinfo ", connection)
adapter.Fill(dt)
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "pdfname"
ComboBox1.ValueMember = "idpdf"
dt.Constraints.Add("Primary", dt.Columns("idpdf"), True)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim filename As String = ComboBox1.Text
Dim row As DataRow = dt.Rows.Find(ComboBox1.SelectedValue)
Dim file_data() As Byte = CType(row(2), Byte())
Dim fs As New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)
fs.Write(file_data, 0, file_data.Length)
fs.Close()
Process.Start(filename)
End Sub
答案 0 :(得分:2)
要定义表的主键,应使用CORS属性,而不是用于外键或唯一约束的PrimaryKey属性。
dt.PrimaryKey = New DataColumn() {dt.Columns("idpdf"))