使用 c# 和 ms access 进行编辑

发布于 2024-12-08 18:08:53 字数 2898 浏览 0 评论 0原文

我有一个问题,我尝试了这段代码:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        conn.Open();
        OleDbCommand cmd = new OleDbCommand("UPDATE tbl_Fullname SET Firstname=@firstn,Lastname=@lastn,Middlename=@midn WHERE fnID=@idn", conn);

        cmd.Parameters.Add("@idn", OleDbType.VarChar).Value = textBox5.Text;
        cmd.Parameters.Add("@firstn", OleDbType.VarChar).Value = textBox1.Text;
        cmd.Parameters.Add("@lastn", OleDbType.VarChar).Value = textBox2.Text;
        cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
        cmd.ExecuteNonQuery();
        conn.Close();
    }

没有错误,但是当我检查我的数据库访问权限时,没有任何变化。

这是更新和编辑的确切代码吗?

这是我的整个代码:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {

        OleDbCommand cmd = new OleDbCommand("INSERT INTO tbl_Fullname (Lastname,Firstname,Middlename) VALUES (@first,@last,@midn)",conn);

        conn.Open();
        cmd.Parameters.Add("@first", OleDbType.VarChar).Value = textBox1.Text;
        cmd.Parameters.Add("@last", OleDbType.VarChar).Value = textBox2.Text;
        cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
        cmd.ExecuteNonQuery();
        conn.Close();
    }

    private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
    {
        conn.Open();
        OleDbCommand cmd2 = new OleDbCommand("SELECT fnID,Lastname,Firstname,Middlename FROM tbl_Fullname WHERE fnID= @id", conn);


        cmd2.Parameters.Add("@id", OleDbType.VarChar).Value = textBox4.Text;
        try
        {
            OleDbDataReader dr = cmd2.ExecuteReader();
            if (dr.Read())
            {
                textBox1.Text = dr[1].ToString();
                textBox2.Text = dr[2].ToString();
                textBox3.Text = dr[3].ToString();
                textBox5.Text = dr[0].ToString();

            }
            else
            {
                MessageBox.Show("No result");
            }

        }
        catch (Exception ex)
        {


            MessageBox.Show(ex.Message);
        }
        conn.Close();

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        conn.Open();
        OleDbCommand cmd = new OleDbCommand("UPDATE tbl_Fullname SET Firstname=@firstn,Lastname=@lastn,Middlename=@midn WHERE fnID=@idn", conn);

        cmd.Parameters.Add("@idn", OleDbType.VarChar).Value = textBox5.Text;
        cmd.Parameters.Add("@firstn", OleDbType.VarChar).Value = textBox1.Text;
        cmd.Parameters.Add("@lastn", OleDbType.VarChar).Value = textBox2.Text;
        cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}

}

我希望有人可以帮助我。我非常需要它。先感谢您。

i have a problem, i tried this code :

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        conn.Open();
        OleDbCommand cmd = new OleDbCommand("UPDATE tbl_Fullname SET Firstname=@firstn,Lastname=@lastn,Middlename=@midn WHERE fnID=@idn", conn);

        cmd.Parameters.Add("@idn", OleDbType.VarChar).Value = textBox5.Text;
        cmd.Parameters.Add("@firstn", OleDbType.VarChar).Value = textBox1.Text;
        cmd.Parameters.Add("@lastn", OleDbType.VarChar).Value = textBox2.Text;
        cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
        cmd.ExecuteNonQuery();
        conn.Close();
    }

there was no error but when i ckecked my db access there is nothing changed.

is this the exact code for updatint and editing?

here is my whole code:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {

        OleDbCommand cmd = new OleDbCommand("INSERT INTO tbl_Fullname (Lastname,Firstname,Middlename) VALUES (@first,@last,@midn)",conn);

        conn.Open();
        cmd.Parameters.Add("@first", OleDbType.VarChar).Value = textBox1.Text;
        cmd.Parameters.Add("@last", OleDbType.VarChar).Value = textBox2.Text;
        cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
        cmd.ExecuteNonQuery();
        conn.Close();
    }

    private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
    {
        conn.Open();
        OleDbCommand cmd2 = new OleDbCommand("SELECT fnID,Lastname,Firstname,Middlename FROM tbl_Fullname WHERE fnID= @id", conn);


        cmd2.Parameters.Add("@id", OleDbType.VarChar).Value = textBox4.Text;
        try
        {
            OleDbDataReader dr = cmd2.ExecuteReader();
            if (dr.Read())
            {
                textBox1.Text = dr[1].ToString();
                textBox2.Text = dr[2].ToString();
                textBox3.Text = dr[3].ToString();
                textBox5.Text = dr[0].ToString();

            }
            else
            {
                MessageBox.Show("No result");
            }

        }
        catch (Exception ex)
        {


            MessageBox.Show(ex.Message);
        }
        conn.Close();

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        conn.Open();
        OleDbCommand cmd = new OleDbCommand("UPDATE tbl_Fullname SET Firstname=@firstn,Lastname=@lastn,Middlename=@midn WHERE fnID=@idn", conn);

        cmd.Parameters.Add("@idn", OleDbType.VarChar).Value = textBox5.Text;
        cmd.Parameters.Add("@firstn", OleDbType.VarChar).Value = textBox1.Text;
        cmd.Parameters.Add("@lastn", OleDbType.VarChar).Value = textBox2.Text;
        cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}

}

i hope someone can help me. i need it badly. thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-12-15 18:08:53

MSDN 说:

当 CommandType 设置为 Text 时,OLE DB .NET 提供程序不支持将参数传递给 SQL 语句或 OleDbCommand 调用的存储过程的命名参数。在这种情况下,必须使用问号 (?) 占位符。例如:

从客户中选择 * WHERE CustomerID = ?

因此,将 OleDbParameter 对象添加到 OleDbParameterCollection 的顺序必须直接对应于命令文本中参数的问号占位符的位置。

因此,请这样做:

 private void button1_Click(object sender, RoutedEventArgs e)
    {
        conn.Open();
        OleDbCommand cmd = new OleDbCommand("UPDATE tbl_Fullname SET Firstname=?,Lastname=?,Middlename=? WHERE fnID=?", conn);

        cmd.Parameters.Add("@firstn", OleDbType.VarChar).Value = textBox1.Text;
        cmd.Parameters.Add("@lastn", OleDbType.VarChar).Value = textBox2.Text;
        cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
        cmd.Parameters.Add("@idn", OleDbType.VarChar).Value = textBox5.Text;
        cmd.ExecuteNonQuery();
        conn.Close();
    }

请注意,参数的顺序在这里很重要!

MSDN says:

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ?

Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

So make it that way:

 private void button1_Click(object sender, RoutedEventArgs e)
    {
        conn.Open();
        OleDbCommand cmd = new OleDbCommand("UPDATE tbl_Fullname SET Firstname=?,Lastname=?,Middlename=? WHERE fnID=?", conn);

        cmd.Parameters.Add("@firstn", OleDbType.VarChar).Value = textBox1.Text;
        cmd.Parameters.Add("@lastn", OleDbType.VarChar).Value = textBox2.Text;
        cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
        cmd.Parameters.Add("@idn", OleDbType.VarChar).Value = textBox5.Text;
        cmd.ExecuteNonQuery();
        conn.Close();
    }

Note that order of parameters is important here!

七色彩虹 2024-12-15 18:08:53

在 SQL 周围添加 try/catch 并输出 sqlCommand 异常。

另请参阅此链接 http://idealprogrammer。 com/net-languages/code-samples/sql-command-update-statement-source-code/ 我发现你可以在哪里看到如何插入、更新等。适当地。

Add a try/catch around your SQL and output the sqlCommand Exeption.

Also see this link http://idealprogrammer.com/net-languages/code-samples/sql-command-update-statement-source-code/ I found where you can see how to insert,update,etc. properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文