- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个数据库表(在这个例子中是空的)
create table words
(
id int not null,
word nvarchar(50) not null
)
和我正在填充的 DataGridView
:
private SqlConnection _conn;
private SqlDataAdapter _wordsAdapter;
private DataTable _wordsDataTable = new DataTable();
private DataGridView _tblWords;
private void FillWords()
{
_wordsAdapter = new SqlDataAdapter(new SqlCommand("select id, word from words", _conn));
_wordsAdapter.Fill(_wordsDataTable);
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "id";
column.Name = "id";
_tblWords.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "word";
column.Name = "word";
_tblWords.Columns.Add(column);
_tblWords.DataSource = _wordsDataTable;
}
private void Update()
{
_wordsAdapter.InsertCommand = new SqlCommand("insert into words (id, word) values (@id, @word)", _conn);
SqlParameter p = _wordsAdapter.InsertCommand.Parameters.Add("@id", SqlDbType.Int);
p.SourceColumn = "id";
p.SourceVersion = DataRowVersion.Original;
p = _wordsAdapter.InsertCommand.Parameters.Add("@word", SqlDbType.NVarChar);
p.SourceColumn = "word";
p.SourceVersion = DataRowVersion.Original;
_wordsAdapter.Update(_wordsDataTable);
}
然后我用一些值填充单行 _tblWords 并调用 Update()
。并收到此消息:
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Additional information: Cannot insert the value NULL into column 'id', table 'DB.MDF.dbo.words'; column does not allow nulls. INSERT fails.
为什么“id”的值不是从 DataTable 中获取的?我做错了什么?
更新:在 Update() 函数的开头插入此代码后,一切正常:
private void Update()
{
_wordsDataTable.Rows.Clear();
_wordsDataTable.Rows.Add(0, "xxx");
...
所以只有当我通过DataGridView填充DataTable时才会出现这个问题。为什么?!
最佳答案
我猜测问题与 DataGridView 中的最后一个空行有关。
你可以尝试这样的事情而不是最后的 _wordsAdapter.Update(_wordsDataTable);
行
var haveDbNull = _wordsDataTable.Rows.Cast<DataRow>().ToLookup(r => r.ItemArray.Contains(DBNull.Value));
Debug.Print("have DbNull values: " + haveDbNull[true].Count()); // check this number in the Debug Output window
Debug.Print("don't have DbNull values: " + haveDbNull[false].Count());
_wordsAdapter.Update(haveDbNull[false].ToArray()); // to update just the rows that dont have DBNull values
关于c# - 在 SqlDataAdapter.Update() 中获取错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382966/
我正在编写一个程序,从 SQL 数据库中提取数据并将其输入到 Excel 中。我一切正常,只是我注意到在 Excel 中返回的行与在 SQL 中看到的不匹配。填充 DataTable 对象时,最后
我正在尝试更新 TableTwo使用 DataTable使用 TableOne 构建. 表之间的关系是一个名为 TableOneId 的外部列里面 TableTwo . 我使用以下代码示例来完成这项工
看来我的代码不完整或者我的语法有误,但我尽力想出了某种解决方案,但到目前为止没有成功......所以这就是我正在尝试做的事情:我有几个下拉框,想将每个下拉框的选定值分配给表适配器中的值。到目前为止,这
SqlDataAdapter 是否打开自己的连接? private DataTable UpdateOxa(ProductCatalogSyncData syncDataModel,
我正在尝试批量插入/更新 SqlDataAdapter。当我设置 UpdateBatchSize = 1 时,它可以工作,但将其设置为 2 会出现异常“指定的参数名称‘Id’无效。”。 using (
我有两个表,User 和 UserReputation。我想用 SqlDataAdapter 获取数据。 SqlConnection conn = new SqlConnection(
大家好,我有这个 SqlDataAdapter 可以找到最高值: DataTable dt = new DataTable(); SqlDataAdapter SDA = ne
我有一个问题。我整天都在尝试解决它,但我真的被困住了。我正在使用 VS2010 和 SQL Server 2012(相当糟糕的组合),我正在尝试执行一段相当简单的 C# 和 ASP.net 代码,如下
我想使用 SqlDataAdapter 将数据从一个数据库表插入到另一个数据库表,但在从另一个表导入行后没有效果 代码 DataTable table1 = objDataset1.Tables[0]
我想知道 SqlDataAdapter 是如何在内部工作的,尤其是当使用 UpdateCommand 来更新一个巨大的 DataTable 时(因为它通常比仅发送要快得多来自循环的 sql 语句)。
我正在尝试在构建 sqldataadapter 后运行更新语句。我的表中有一个名为 INIT_PHASE 的列,如果 INIT_PHASE 为空或没有数据,那么我想将其设置为 1。我已经尝试过,但我似
我在 MS SQL 2005 中有一个带有主键的表,它有几十万条记录。当我在 Management Studio 中查询它以获取记录时,它带来的速度非常快,但是当我使用下面的代码找到它时,它需要很多秒
假设您调用了 SQlDataAdapter 的更新方法: MyDataSet.Tables[0].DefaultView.RowStateFilter = DataViewRowSta
我知道通常推荐的从存储过程填充 DataSet 实例的方法是使用 SqlDataAdapter.Fill(DataSet)。我能找到的每个答案都说要以开箱即用的方式使用它。我会正常授予的,它很好用。
我正在使用 VS2012 和 SQL Server Express 2008。我已经归结我的连接/查询以尝试找出为什么我的 DataSet 没有被填充。连接成功完成,没有抛出异常,但是适配器没有填充D
我的问题:有没有办法像这样用一个 SqlDataAdapter 运行多个存储过程 adapter = new SqlDataAdapter("ProcforselectUserTableWhere ;
我做了以下事情: SqlConnection conn = new SqlConnection("... my connection string ..."); dataAdapter = new S
所以,我有一个必须维护的内部数据访问框架,并且我要向其中添加事务。我在放入一个包时遇到问题:Transactions、SqlDataAdapters、SqlCommandBuilders 以及主键定义
然而,我一直在尝试将 SQL 数据库加载到 C++ 中的数据表中;它似乎不想工作。不过,随着 DataReader 的工作,连接正在工作。这是我的代码 void importDatabase() {
所以我试图用来自 MSSQL 查询的数据填充 DataTable,但出于某种原因它完全拒绝工作,这让我非常恼火。 当我使用 SqlDataReader 和 Read() 遍历结果时,我得到了结果,甚至
我是一名优秀的程序员,十分优秀!