执行非查询错误

时间:2015-07-21 08:37:14

标签: c# sql

我试图通过我的C#代码将一些值插入到表中,但每次执行它时,它都会一直运行到ExecuteNonQuery(),然后我得到一个异常并且它会中断。这是代码:

connection = new SqlCeConnection("Data Source = Book base.sdf");
connection.Open();

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] im = ms.ToArray();

using (SqlCeCommand com = new SqlCeCommand("INSERT INTO Authors (IDN, Surname, Picture, Date of birth, Writer_name) VALUES (@id, @surname, @picture, @date, @name)", connection))
{
    com.Parameters.AddWithValue("@id", id.Text);
    Console.WriteLine(id.Text);

    com.Parameters.AddWithValue("@surname", surname.Text);
    Console.WriteLine(surname.Text);

    com.Parameters.AddWithValue("@picture", im);

    com.Parameters.AddWithValue("@date", dateTimePicker1.Text);
    Console.WriteLine(dateTimePicker1.Text);

    com.Parameters.AddWithValue("@name", name.Text);
    Console.WriteLine(name.Text);

    Console.WriteLine(com.ExecuteNonQuery());

那么,问题可能是什么?

2 个答案:

答案 0 :(得分:3)

也许你需要围绕出生日期的括号:, [Date of birth], Writer_name

答案 1 :(得分:0)

检查idn是否为字符串,并且出生日期是表格中的datetime2。 同时在您的查询中将出生日期放在方括号内。