我正在向DataTable添加一个对象,DataTable是DataSet的Tables集合的一部分。然后我在与前面提到的DataTable相关联的DataAdapter上调用Update。但是,当我调用Update时,它会抛出一个异常,说" NOT NULL约束失败:proxies.port"。我将分享我用来创建数据库,加载它的数据,甚至我正在使用的Proxy对象的构造函数的代码。首先正确回答得到复选标记:)
谢谢!
Public Sub CreateDB()
Dim dbLocation As String = My.Application.Info.DirectoryPath & "\data.s3db"
SQLiteConnection.CreateFile(dbLocation)
Using conn As New SQLiteConnection("DataSource=" & dbLocation & ";Version=3;")
Using cmd As New SQLiteCommand(conn)
conn.Open()
cmd.CommandText = "CREATE TABLE proxies (
ip Text Not NULL,
port Integer Not NULL,
countryName Text Not NULL,
pingTime Integer Not NULL,
status Text Not NULL,
PRIMARY KEY (ip, port),
unique(ip, port)
);
CREATE TABLE [judges] (
[id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[url] TEXT UNIQUE NOT NULL,
[pingTime] INTEGER NOT NULL,
[status] TEXT NOT NULL
);
CREATE TABLE [tests] (
[id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[url] TEXT NULL,
[validationText] TEXT NOT NULL,
[invalidationText] TEXT NOT NULL,
[status] TEXT NOT NULL
)"
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
End Sub
我正在像这样加载这些数据:
Public Sub LoadDataFromDB()
Using connection As New SQLiteConnection(dbConnectionString)
dbDataSet.Tables.Add("proxies")
dbProxyAdapter.Fill(dbDataSet.Tables("proxies"))
Dim cmdBuilder As New SQLiteCommandBuilder(dbProxyAdapter)
dbProxyAdapter.InsertCommand = cmdBuilder.GetInsertCommand
Dim proxy As New Proxy("1.1.1.1:12345")
dbDataSet.Tables("proxies").Rows.Add(proxy)
dbProxyAdapter.Update(dbDataSet.Tables("proxies"))
End Using
End Sub
代理对象构造函数
Public Sub New(proxy As String)
If System.Text.RegularExpressions.Regex.IsMatch(proxy, "(\d{1,3}\.){3}\d{1,3}:\d{2,5}") Then
Me.IP = proxy.Split(":")(0)
Me.Port = proxy.Split(":")(1)
Me.PingTime = 0
Me.Status = ProxyStatus.Unknown
Me.CountryName = "Unknown"
Else
Throw New Exception("Invalid proxy string '" & proxy & "'")
End If
End Sub
答案 0 :(得分:0)
回答了我自己的问题。
通过创建DataRow对象并返回dbDataSet.Tables(" proxies")。NewRow(),然后设置每个列的值为likeo:row.Item(" ip")= value ,最后将此行添加到DataSet中的DataTable,在DataAdapter上调用Update时,满足NOT NULL约束。问题仍然存在,为什么Proxy对象的ItemArray没有正确转换,列名称与Proxy对象的公共属性名称匹配。哦,好吧,我不想考虑它,我的解决方法会做,因为它解决了我的头痛,加了几行代码而不是我希望的......