有什么不对这些参数? [英] What's wrong with these parameters?

查看:116
本文介绍了有什么不对这些参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有7场Access文件:

I have an Access file with 7 fields:

DocID - text - primary
SourceID - text
ReceivedDay - Date/Time
Summary - text
DueDay - Date/Time
Person - text
Status - Yes/No

现在我想更新有以下code这个文件:

Now I want to update this file with the following code:

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();

但我得到一个异常:

But I get an exception:

Data type mismatch in criteria expression.

我该如何解决这个问题?

How can I fix this?

编辑:我解决了这个问题,采用了不同的方法:

I fixed this, using a different approach:

我建一个这样的查询:

INSERT INTO Docs
   (DocID, SourceID, ReceivedDay, Summary, Person, DueDay, Status)
VALUES (?, ?, ?, ?, ?, ?, ?)

,然后用一个TableAdapter来调用它:

and then used a TableAdapter to call it:

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);

更简单,现在工作得很好。谢谢大家。

Much more simple, and It works fine now. Thank you all

推荐答案

简单的要求Google ,我估计超过10000个点击率也相当IM pressive。你的论点的我不认为......的是无效的,直到你证明了这一点。

Simply ask google, I guess more than 10000 hits is quite impressive. Your argument "I don't think that..." is not valid until you proved it.

这是什么 MSDN 说:

在OLE DB.NET提供程序不支持将参数传递到SQL语句或由的OleDbCommand 当CommandType设置为文本调用的存储过程的命名参数。在这种情况下,必须使用问号()的占位符。例如:

The OLE DB.NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE
CustomerID = ?


  
  

因此​​,在这种 OleDbParameter 对象添加到订单的 OleDbParameterCollection 必须直接对应的位置问号占位符的命令文本参数。

Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

这篇关于有什么不对这些参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆