C#OleDB SQL插入命令

时间:2012-11-18 17:20:02

标签: c# sql ms-access oledb

我正在尝试使用C#和Visual Studio 2012在我的MS Access数据库中实现SQL INSERT命令。

但在那之后,当我打开Access数据库时,即使通过insert

之后显示成功弹出窗口成功创建executeNonQuery()命令也没有更新

请您告诉我如何将此插入SQL命令工作?

这是我的代码

OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = @"INSERT INTO STUDENT(CHI_NAME, ENG_NAME, NICK_NAME, 
                                        BD_YYYY, BD_MM, BD_DD,
                                        HOME_TEL, NO_OF_CHILD, PARENT_EMAIL,
                                        SEC_EMAIL, STUDENT_ADDR, DISTRICT, 
                                        MOTHER_NAME, MOTHER_TEL, MOTHER_OCCU, 
                                        FATHER_NAME, FATHER_TEL, FATHER_OCCU,
                                        E_NAME, E_TEL, E_RELATIONSHIP, REMARKS) 
                    VALUES(@NEW_CHI_NAME, @NEW_EN  G_NAME, @NEW_NICK_NAME, 
                           @NEW_BD_YYYY, @NEW_BD_MM .....);"

cmd.CommandType = CommandType.Text;

cmd.Parameters.AddWithValue("@NEW_CHI_NAME", chi_name);
cmd.Parameters.AddWithValue("@NEW_ENG_NAME", eng_name);
cmd.Parameters.AddWithValue("@NEW_NICK_NAME", nick_name);            
cmd.Parameters.AddWithValue("@NEW_BD_YYYY", bd_year);
cmd.Parameters.AddWithValue("@NEW_BD_MM", bd_month);
....

cmd.Connection = connection;
connection.Open();
cmd.Transaction = connection.BeginTransaction();
int rows= cmd.ExecuteNonQuery();

if(rows > 0)
{
     MessageBox.Show("Insert New Client Success!");
}
else
{
    MessageBox.Show("Insert New Client Failed!"); 
}

cmd.Transaction.Commit();
connection.Dispose();
connection.Close();

2 个答案:

答案 0 :(得分:2)

Solution Explorer中,右键单击您的访问文件,选择Properties,然后将Copy To Output Directory设置为Copy if Newer

答案 1 :(得分:0)

转到调试目录,你会发现你在那里复制了accdb文件。打开并验证。