【问题标题】:Adding data in the database using microsoft access and OleDb in C#在 C# 中使用 microsoft access 和 OleDb 在数据库中添加数据
【发布时间】:2013-03-23 11:52:10
【问题描述】:

我是 OleDb 库的新手,我想使用该库将文本表单文本框添加到数据库中。 我的代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private OleDbConnection conn = new OleDbConnection();
    private void button1_Click(object sender, EventArgs e)
    {
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Project\Learning\Visual C#\Form\WindowsFormsApplication1\WindowsFormsApplication1\Test.mdb";
        string NAME = textBox1.Text;
        conn.Open();
        OleDbCommand cmmd = new OleDbCommand("INSERT into student(NAME)" + "VALUES(@NAME)", conn);  
        if (conn.State == ConnectionState.Open)
        {
            cmmd.Parameters.Add("@NAME", OleDbType.Char, 20);
            cmmd.Parameters["@NAME"].Value = NAME;
            try
            {
                cmmd.ExecuteNonQuery();
                MessageBox.Show("DATA ADDED");
                conn.Close();
            }
            catch (OleDbException expe)
            {
                MessageBox.Show(expe.Source);
            }
        }
        else
        {
            MessageBox.Show("CON FAILED");
        }
    }
}

但它不起作用。 我在 C# 中找不到 OleDbCommand 的良好参考。
如何在我的代码中使用 OleDbCommand

【问题讨论】:

  • 什么不起作用?您在哪一行得到异常,异常是什么?
  • 在我的 try catch 中,代码放入 catch 部分并在 Message Box 中显示“Microsoft Access 数据库引擎”。出了什么问题?
  • 是的,但结果相同!
  • 不,您是否更改了此代码的任何部分?
  • 我更新了我的答案,我无法测试,但我认为这对你有用。

标签: c# ms-access visual-studio-2012 oledb


【解决方案1】:

解决了

public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Project\Learning\Visual C#\Form\WindowsFormsApplication2\WindowsFormsApplication2\Test.mdb";
        conn.Open();
        string Name = textBox1.Text;
        OleDbCommand cmmd = new OleDbCommand("INSERT INTO table1 (student) Values(@Name)", conn);
        if (conn.State == ConnectionState.Open)
        {
            cmmd.Parameters.Add("@Name", OleDbType.VarWChar, 20).Value = Name;
            try
            {
                cmmd.ExecuteNonQuery();
                MessageBox.Show("DATA ADDED");
                conn.Close();
            }
            catch (OleDbException expe)
            {
                MessageBox.Show(expe.Message);
                conn.Close();
            }
        }
        else
        {
            MessageBox.Show("CON FAILED");
        }
    }

【讨论】:

  • 很高兴看到您解决了这个问题,如果您稍等片刻,您可以将其标记为答案。快乐编码:)
【解决方案2】:

也许这段代码可以帮助你

      OleDbConnection dbConnection = new OleDbConnection(CONNECTION_STRING);

        string commandString = 
        "INSERT INTO MeetingEntries (Subject, Location, Start Date, End Date, Enable   Alarm, Repeat Alarm, Reminder, Repetition Type)" + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

        OleDbCommand commandStatement = new OleDbCommand(commandString, dbConnection);

        commandStatement.Parameters.Add("@Subject", OleDbType.VarWChar, 30).Value = currentEntry.Subject;
        commandStatement.Parameters.Add("@Location", OleDbType.VarWChar, 50).Value = currentEntry.Location;
        commandStatement.Parameters.Add("@Start Date", OleDbType.Date, 40).Value = currentEntry.StartDateTime.Date;
        commandStatement.Parameters.Add("@End Date", OleDbType.Date, 40).Value = currentEntry.EndDateTime.Date;
        commandStatement.Parameters.Add("@Enable Alarm", OleDbType.Boolean, 1).Value = currentEntry.IsAlarmEnabled;
        commandStatement.Parameters.Add("@Repeat Alarm", OleDbType.Boolean, 1).Value = currentEntry.IsAlarmRepeated;
        commandStatement.Parameters.Add("@Reminder", OleDbType.Integer, 2).Value = currentEntry.Reminder;
        commandStatement.Parameters.Add("@Repetition Type", OleDbType.VarWChar, 10).Value = currentEntry.Repetition;

        dbConnection.Open();
        commandStatement.ExecuteNonQuery();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-14
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多