通过从文本框中插入值出现问题 [英] Problems by inserting values from textboxes

查看:73
本文介绍了通过从文本框中插入值出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将当前日期插入数据库,并且始终收到消息(当我按表单上的按钮保存到我的Access数据库时),条件表达式中的数据类型不正确。
代码:

I'm trying to insert the current date to the database and i allways get the message(when i press the button on the form to save to my access database), that the data type is incorect in the conditional expression. the code:

string conString = "Provider=Microsoft.Jet.OLEDB.4.0;"
         + "Data Source=C:\\Users\\Simon\\Desktop\\save.mdb";

            OleDbConnection empConnection = new OleDbConnection(conString);


            string insertStatement = "INSERT INTO obroki_save "
                                 + "([ID_uporabnika],[ID_zivila],[skupaj_kalorij]) "
                                 + "VALUES (@ID_uporabnika,@ID_zivila,@skupaj_kalorij)";

            OleDbCommand insertCommand = new OleDbCommand(insertStatement, empConnection);

            insertCommand.Parameters.Add("@ID_uporabnika", OleDbType.Char).Value = users.iDTextBox.Text;
                            insertCommand.Parameters.Add("@ID_zivila", OleDbType.Char).Value = iDTextBox.Text;
            insertCommand.Parameters.Add("@skupaj_kalorij", OleDbType.Char).Value = textBox1.Text;
            empConnection.Open();

            try
            {
               int count = insertCommand.ExecuteNonQuery();
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                empConnection.Close();
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                textBox5.Clear();
            }

我现在已经删除了日期,(我已将访问粘贴到日期上) ,仍然存在相同的问题。第一行可以吗? users.idtextbox.text?
请帮忙!

I have now cut out the date,( i made access paste the date ), still there is the same problem. Is the first line ok? users.idtextbox.text? Please help !

推荐答案

尝试更改

insertCommand.Parameters.Add( @ datum,OleDbType.Char).Value = DateTime.Now;

to

 `insertCommand.Parameters.Add("@datum", OleDbType.Char).Value 
      = DateTime.Now.ToString("dd MMM yyyy HH:mm");`

(或其他一些可接受的日期格式)

( or some other acceptable date format)

这篇关于通过从文本框中插入值出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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