0

在以下示例中,我必须输入所有详细信息才能将数据保存在数据库中。是否可以将一些文本框留空保存然后使用更新命令参数来填写其余数据?例如插入ID,AgeGroup,Gender,将照片和简历留空并将其保存在Access数据库中?如果您不介意,有人可以提供一个例子。我给你的描述我不知道如何用技术术语来描述?提前感谢您的帮助。

private void btnInsert_Click(object sender, EventArgs e)
    {

        OleDbCommand cmd = new OleDbCommand(@"INSERT INTO Table1
                                    (ID, AgeGroup, Gender, CriminalOffence, photo, CV)   
                              VALUES(@ID, @AgeGroup, @Gender, @CriminalOffence, @photo, @Resume)", con);
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@ID",textBox1.Text);
        cmd.Parameters.AddWithValue("@AgeGroup", comboBox1.Text);
        cmd.Parameters.AddWithValue("@Gender", comboBox2.Text);
        cmd.Parameters.AddWithValue("@CriminalOffence", OleDbType.WChar).Value = str;
        if (pictureBox1.Image != null)
        {
            //using MemoryStream:
            ms = new MemoryStream();
            pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
            byte[] photo_aray = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(photo_aray, 0, photo_aray.Length);
            cmd.Parameters.AddWithValue("@photo", photo_aray);
        }
        cmd.Parameters.AddWithValue("@Resume", richTextBox1.Text);
        con.Open();
        cmd.ExecuteNonQuery();
         con.Close(); 

VS 2010 C# 访问 2003

4

3 回答 3

1

提交前添加检查语句:

例如:

 cmd.Parameters.AddWithValue("@ID",  string.IsNullOrEmpty(textBox1.Text.Trim())?  DBNull.Value:textBox1.Text.Trim());
于 2013-04-26T11:56:55.363 回答
0

您需要传递值来处理错误“没有为一个或多个必需参数提供值”。

它可能是 DBNull 或空字符串。

于 2013-04-26T12:26:31.350 回答
0

have you set the allow null property to true for the columns that you wanna update later in your database? if not do it and the current code of yours will work fine i guess..

string command = stirng.Format("INSERT INTO Table1(ID, AgeGroup) VALUES ({0},{1})",TextBox1.Text,ComboBox1.Text)
OleDbCommand cmd = new OleDbCommand(command, con)
于 2013-04-26T12:28:52.967 回答