簡體   English   中英

在C#中使用microsoft訪問和OleDb在數據庫中添加數據

[英]Adding data in the database using microsoft access and OleDb in C#

我是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

解決了

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");
        }
    }

可能是這段代碼可以幫到你

      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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM