需要帮助T_T,如何使用C#插入Ole对象(特别是pdf文件)? [英] Need help T_T, how can i insert Ole Object(specifically pdf file) using c#?
问题描述
OleDbConnectionStringBuilder ocsb;
OleDbConnection conn;
OleDbCommand cmd;
try
{
ocsb = new OleDbConnectionStringBuilder();
ocsb["Provider"] = "Microsoft.Jet.OLEDB.4.0";
ocsb["Data Source"] = "CMSDbase.mdb";
string connStr = ocsb.ConnectionString;
conn = new OleDbConnection(connStr);
conn.Open();
string iSql = "INSERT INTO FileTable VALUES (?,?,?,?,?,?,?)";
cmd = new OleDbCommand(iSql, conn);
cmd.Parameters.Add("p1", OleDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("p2", OleDbType.VarChar).Value = comboBox1.Text;
cmd.Parameters.Add("p3", OleDbType.VarChar).Value = comboBox2.Text;
cmd.Parameters.Add("p4", OleDbType.VarChar).Value = comboBox3.Text;
cmd.Parameters.Add("p5", OleDbType.Date).Value = dateFrom.Value;
cmd.Parameters.Add("p6", OleDbType.Date).Value = dateTo.Value;
//cmd.Parameters.Add("p7", OleDbType.VarChar).Value = browseText1.Text; //here is my problem T_T
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Data Saved!", "data saved!", MessageBoxButtons.OK);
} catch (Exception ex){
MessageBox.Show(ex.ToString(),"Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
[edit]已添加代码块-OriginalGriff [/edit]
[edit]Code block added - OriginalGriff[/edit]
推荐答案
不要使用Parameters.Add:请改用Paramaters.AddWithValue.这样,它应该可以计算出转换本身,或者给您一个错误,说明无法执行的操作:
Don''t use Parameters.Add: use Paramaters.AddWithValue instead. That way it should work out the conversion itself, or give you a error explaingin what it can''t do:
cmd = new OleDbCommand(iSql, conn);
cmd.Parameters.AddWithValue("p1", textBox1.Text);
cmd.Parameters.AddWithValue("p2", comboBox1.Text);
...
如果您仍然遇到问题,那么我们需要知道错误消息是什么.
If you still get a problem, then we need to know what the error message is.
感谢"AddWithValue"建议...但是还有另一个问题...关于我的Ole对象的信息,我无法将其插入到表中...我使用OpenFileDialog浏览了可能是我的OLE对象的PDF文件,该文件将插入到我的msaccess数据库中的OLE对象列中.我不知道如何使用c#插入OLE对象的完整顺序...
thanks for the "AddWithValue" advice... but there is another problem... its about my Ole Object, i cant insert it with my table... i used OpenFileDialog to browse the PDF file that suppose to be my OLE object that will be inserted to my OLE object column in my msaccess database. i dont know the complete sequence in how to insert OLE object using c#...
这是我的新代码...
here is my new code...
cmd.Parameters.AddWithValue(textBox1.Text, OleDbType.VarChar);
cmd.Parameters.AddWithValue(comboBox1.Text, OleDbType.VarChar);
cmd.Parameters.AddWithValue(comboBox2.Text, OleDbType.VarChar);
cmd.Parameters.AddWithValue(comboBox3.Text, OleDbType.VarChar);
cmd.Parameters.AddWithValue(dateTimePicker1.Value.ToString(), OleDbType.DBDate);
cmd.Parameters.AddWithValue(dateTimePicker2.Value.ToString(), OleDbType.DBDate);
cmd.Parameters.AddWithValue(browseText1.Text, OleDbType.VarChar);
错误消息显示:
System.Data.OleDb.OleDbException:查询值和目标字段的数量不相同.
我认为问题之一就是这部分...
cmd.Parameters.AddWithValue(browseText1.Text,OleDbType.VarChar);
BrowseText1是我在其中显示pdf文件位置/路径的文本框.
Error message says:
System.Data.OleDb.OleDbException: Number of query values and destination fields are not the same.
i think one of the problems is this part...
cmd.Parameters.AddWithValue(browseText1.Text,OleDbType.VarChar);
browseText1 is the textbox where i display the location/path of my pdf file.
这篇关于需要帮助T_T,如何使用C#插入Ole对象(特别是pdf文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!