【发布时间】:2010-11-16 00:29:50
【问题描述】:
我有一个包含 7 个字段的 Access 文件:
DocID - text - primary
SourceID - text
ReceivedDay - Date/Time
Summary - text
DueDay - Date/Time
Person - text
Status - Yes/No
现在我想用以下代码更新这个文件:
const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\DocMan.mdb;Persist Security Info=True";
const string InsertQuery = "INSERT Into Docs(DocID,ReceivedDay,Summary,Person,DueDay,Status,SourceID) Values(@DocID,@ReceivedDay,@Summary,@Person,@DueDay,@Status,@SourceID)";
string DocID = textBox1.Text;
string SourceID = comboBox1.SelectedIndex.ToString();
DateTime ReceivedDay = dateTimePicker1.Value;
string Summary = richTextBox1.Text;
string Person = textBox2.Text;
DateTime DueDay = dateTimePicker2.Value;
bool Status = false;
OleDbConnection cnn = new OleDbConnection(ConnectionString);
cnn.Open();
OleDbCommand cmd = new OleDbCommand(InsertQuery, cnn);
cmd.Parameters.AddWithValue("@DocID", DocID);
cmd.Parameters.AddWithValue("@SourceID", SourceID);
cmd.Parameters.AddWithValue("@ReceivedDay", ReceivedDay);
cmd.Parameters.AddWithValue("@Summary", Summary);
cmd.Parameters.AddWithValue("@Person", Person);
cmd.Parameters.AddWithValue("@DueDay", DueDay);
cmd.Parameters.AddWithValue("@Status", Status);
cmd.ExecuteNonQuery();
cnn.Close();
但我得到一个例外:
Data type mismatch in criteria expression.
我该如何解决这个问题?
编辑:我用不同的方法解决了这个问题:
我构建了一个这样的查询:
INSERT INTO Docs
(DocID, SourceID, ReceivedDay, Summary, Person, DueDay, Status)
VALUES (?, ?, ?, ?, ?, ?, ?)
然后用一个TableAdapter来调用它:
string DocID = textBox1.Text;
string SourceID = comboBox1.SelectedIndex.ToString();
DateTime ReceivedDay = dateTimePicker1.Value.Date;
string Summary = richTextBox1.Text;
string Person = textBox2.Text;
DateTime DueDay = dateTimePicker2.Value.Date;
bool Status = false;
DocManDataSetTableAdapters.DocsTableAdapter docsTableAdapter = new DocManDataSetTableAdapters.DocsTableAdapter();
docsTableAdapter.InsertQuery(DocID,SourceID,ReceivedDay,Summary,Person,DueDay,false);
简单多了,现在可以正常使用了。谢谢大家
【问题讨论】:
-
能否请您显示 InsertQuery 的文本?
-
是否有任何列的限制?
-
SourceID 有外来约束,但即使我删除它,错误仍然出现
-
看起来是时候进行实验了:1) 以交互方式运行插入,使用相似的数据。 2) 从插入中删除所有可为空的列。