Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加到数据库oledb c#_C#_Sql_Database_Ms Access_Oledb - Fatal编程技术网

添加到数据库oledb c#

添加到数据库oledb c#,c#,sql,database,ms-access,oledb,C#,Sql,Database,Ms Access,Oledb,在搜索了大约一个小时后,这似乎是使用oledb库向access数据库插入记录的正确方法,但是它对我不起作用,请帮助 InitializeComponent(); System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(); // TODO: Modify the connection string and include any

在搜索了大约一个小时后,这似乎是使用oledb库向access数据库插入记录的正确方法,但是它对我不起作用,请帮助

        InitializeComponent();
        System.Data.OleDb.OleDbConnection conn = new
        System.Data.OleDb.OleDbConnection();
        // TODO: Modify the connection string and include any
        // additional required properties for your database.
        conn.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = \\crd-a555-015.occ.local\c$\Users\james.piper\Documents\Visual Studio 2015\Projects\Project V1\Project Database.accdb";
        try
        {
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO Work_Done (employee,client,project,task,hours)" + " VALUES (@employee,@client,@project,@task,@hours)";
            cmd.Parameters.AddWithValue("@employee", user.employee);
            cmd.Parameters.AddWithValue("@client", listBox1.SelectedItem.ToString());
            cmd.Parameters.AddWithValue("@project", listBox2.SelectedItem.ToString());
            cmd.Parameters.AddWithValue("@task", listBox3.SelectedItem.ToString());
            cmd.Parameters.AddWithValue("@hours", listBox4.SelectedItem.ToString());



            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("sql insert fail");
        }

我会这样写代码:

var connectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = \\crd-a555-015.occ.local\c$\Users\james.piper\Documents\Visual Studio 2015\Projects\Project V1\Project Database.accdb";
var query = "INSERT INTO Work_Done (employee,client,project,task,hours) VALUES (@employee,@client,@project,@task,@hours)";
using (var conn = new OleDbConnection(connectionString))
{             
    using(var cmd = new OleDbCommand(query, conn))
    {
        // No need to specifiy command type, since CommandType.Text is the default

        // I'm assuming, of course, your parameter data types. You should change them if my assumptions are wrong.
        cmd.Parameters.Add("@employee", OleDbType.Integer).Value = user.employee;
        cmd.Parameters.Add("@client", OleDbType.Integer).Value = Convert.ToInt32(listBox1.SelectedItem);
        cmd.Parameters.Add("@project", OleDbType.Integer).Value =  Convert.ToInt32(listBox2.SelectedItem);
        cmd.Parameters.Add("@task", OleDbType.Integer).Value = Convert.ToInt32(listBox3.SelectedItem);
        cmd.Parameters.Add("@hours", OleDbType.Integer).Value = Convert.ToInt32(listBox4.SelectedItem);

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show($"sql insert fail: {ex}");
        }
    }                
}
主要的变化是:

  • 对实现
    IDisposable
    接口的类的每个实例使用
    Using
    语句
  • 使用带参数的构造函数使代码更短(更可读,IMHO)
  • 请注意,OleDbCommand的构造函数还具有OleDbConnection对象。在代码中,您没有指定与命令的活动连接
  • 使用
    Add
    而不是
    AddWithValue
    添加参数。阅读,找出原因

  • 你需要定义“不起作用”。你有错误吗?意外行为?有些事情没有像你想的那样执行?我们需要一些更详细的信息。您收到了什么错误消息?据我所知,OleDb不支持命名参数,只支持序数参数。因此,您应该将查询更改为使用问号(
    )而不是参数名。此外,您应该习惯使用
    语句中的所有IDisTables-OleDbConnection和OleDbCommand都是IDisTable。@ZoharPeled不受支持,但不会引发错误。它只会将参数名转换为?在幕后。参数必须按索引顺序排列,在本例中,它们是。
    MessageBox.Show(“sql插入失败”)sql插入失败:System.Data.OleDb.OLEDBEException(0x80040E10):参数@client没有默认值。请检查您发送的值。PI做了listbox1.selecteditem.tostring的console.writeline,它打印出了正确的值。我不明白后来它是怎样的一个“null”值。我不知道它应该是什么,也不知道你的代码现在是什么样子。通过心灵感应进行调试是不可能的……作为一个不相关的问题,当我试图复制插入的格式时,向select语句添加参数的正确语法是什么?我得到了这个错误(sql插入失败:System.NullReferenceException:Object reference未设置为对象的实例。在测试时,单击(对象发送方,EventArgs e)在c:\users\james.piper\documents\visual studio 2015\Projects\testing sql\testing sql\Form1.cs:第236行)“代码”中提前感谢