Pasting textbox values to access database

Now i get the problem at the try statement ( int count =insert.......). The compailer says(when i push the buton on the form to save the values in textboxes) that it Failed to convert parameter value from a String to a Int32. code:

private void button1_Click(object sender, EventArgs e) {

       string conString = "Provider=Microsoft.Jet.OLEDB.4.0;"
     + "Data Source=C:UsersSimonDesktopsave.mdb";

        OleDbConnection empConnection = new OleDbConnection(conString);


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

        OleDbCommand insertCommand = new OleDbCommand(insertStatement, empConnection);

        insertCommand.Parameters.Add("@ID_uporabnika", OleDbType.Integer).Value = users.iDTextBox.Text;
        insertCommand.Parameters.Add("@datum", OleDbType.Date).Value = DateTime.Now;
        insertCommand.Parameters.Add("@ID_zivila", OleDbType.Integer).Value = iDTextBox.Text;
        insertCommand.Parameters.Add("@skupaj_kalorij", OleDbType.Integer).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();
        }
    }

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

您将日期作为数据类型Char插入,看起来不正确。


你需要转换你的数据类型:textbox.value保存一个字符串并将其转换,你可以使用int.Parse。

    insertCommand.Parameters.Add("@ID_zivila", OleDbType.Integer).Value = int.Parse(iDTextBox.Text);
    insertCommand.Parameters.Add("@skupaj_kalorij", OleDbType.Integer).Value = int.Parse(textBox1.Text);
链接地址: http://www.djcxy.com/p/21000.html

上一篇: 我们可以在两个泛型列表之间建立关系吗?

下一篇: 粘贴文本框值来访问数据库