无法使用c#将数据插入到访问中

时间:2014-09-04 03:25:42

标签: c#

我尝试了很多次但数据无法插入但没有错误

请帮助

public static void SaveCorresDetails(string cN, string cId, string pId, string rI)
     {
         OleDbConnection myConnection = GetConnection();
         string myQuery = "INSERT INTO RAHANICorrespondent(DateIssue, Action, Recipient, Remarks) VALUES ( '" + cN + "' , '" + cId + "',  '" + pId + "','" + rI + "')";
         OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);

         try
         {
             myConnection.Open();
             myCommand.ExecuteNonQuery();
         }
         catch (Exception ex)
         {
             Console.WriteLine("Exception in DBHandler", ex);
         }
         finally
         {
             myConnection.Close();
         }

     }

这将是类文件 添加数据

protected void Button1_Click(object sender, EventArgs e)
    {
        string n = TextBox1.Text.Trim();
        string c = TextBox2.Text.Trim();

        string p = TextBox3.Text.Trim();
        string r = TextBox4.Text.Trim();


        // save the data into the database
        Data.SaveCorresDetails(n, c, p, r);
        Label6.Text = "Package amount of " + n + " was saved";
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
    }

...谢谢

1 个答案:

答案 0 :(得分:0)

尝试使用db参数。

OleDbConnection connection = new OleDbConnection("");
string query = "INSERT INTO myTable (a,b,c) Values(@a, @b @c)";
OleDbParameter param = new OleDbParameter("a", "Hi man");
param.DbType = ...
OleDbCommand command = new OleDbCommand(query);
command.CommandType = System.Data.CommandType.Text;
int result = command.ExecuteNonQuery();

OleDbConnection connection = new OleDbConnection(""); string query = "INSERT INTO myTable (a,b,c) Values(@a, @b @c)"; OleDbParameter param = new OleDbParameter("a", "Hi man"); param.DbType = ... OleDbCommand command = new OleDbCommand(query); command.CommandType = System.Data.CommandType.Text; int result = command.ExecuteNonQuery();

然后检查您是否收到错误并记录下来,看看这是否有帮助。