在wpf中插入sql查询

发布于 2024-08-14 18:05:14 字数 794 浏览 3 评论 0原文

大家好,我是 wpf 的新人。所以我遇到了问题。如果你帮助我,我会很高兴。预先感谢大家。

我的问题是,无法插入 wpf 数据库内的名称。我该如何修复它?我的代码如下;

private void button1_Click(object sender, RoutedEventArgs e)
{       
    try
    {
        string SqlString = "Insert Into UserInformation(name) Values (?)";
        using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Cell.mdb;Persist Security Info=True"))
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("name", textBox1.Text);
                conn.Open();
                cmd.ExecuteNonQuery();

            } 
        }
    }
    catch (Exception ex)
    {  }
}

Hello everyone i am new in wpf. so i have got problems with it. if you help me, i will be so pleased. thanks everyone in advance.

My problem is, can not insert into name inside database in wpf. how can i fix it? my codes as follows;

private void button1_Click(object sender, RoutedEventArgs e)
{       
    try
    {
        string SqlString = "Insert Into UserInformation(name) Values (?)";
        using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Cell.mdb;Persist Security Info=True"))
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("name", textBox1.Text);
                conn.Open();
                cmd.ExecuteNonQuery();

            } 
        }
    }
    catch (Exception ex)
    {  }
}

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

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

发布评论

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

评论(3

无语# 2024-08-21 18:05:14

尝试使用 cmd.Parameters.AddWithValue("@name", textBox1.Text);

Try to use cmd.Parameters.AddWithValue("@name", textBox1.Text);

倾城°AllureLove 2024-08-21 18:05:14

它打开正确的数据库文件吗?正如人们在评论中建议的那样,将 Visual Studio 设置为在第一次出现异常时中断,或删除异常处理。数据库文件需要存在,并且您需要适当的 JET 驱动程序。

我已经尝试过你的代码,它在这里工作没有任何问题(在 WPF 应用程序或其他应用程序中)。使用命名参数而不是问号是一个很好的建议,但这似乎不是问题。 (我安装了 Office 2007 和 .NET 3.5 SP1,但我怀疑这是否重要)。

您是否使用 WPF 浏览器应用程序 (cbap)?因为如果您这样做,您将无法访问本地文件系统(因此也无法访问数据库)。 WPF 浏览器应用程序以独立的权限运行,就像 Silverlight 浏览器应用程序一样。

Is it opening the right database file? As people have suggested in the comments, set Visual Studio to break on first-chance exceptions, or remove the exception handling. The database file needs to exist, and you need the appropriate JET drivers.

I've tried your code and it works without any problems here (in a WPF application or otherwise). Using named parameters instead of a question mark was a good suggestion, but it doesn't appear to be the problem. (I have Office 2007 and .NET 3.5 SP1 installed, but I doubt that matters).

Are you using a WPF browser application (cbap)? Because you won't be able to access the local file system (and thus the database) if you are. WPF browser applications run with isolated permissions, much like a Silverlight browser application.

瞳孔里扚悲伤 2024-08-21 18:05:14

这里的问题似乎是参数。在命令文本中,您没有指定其名称,但是当您添加它时,它就有一个名称。将命令文本更改为:

Insert Into UserInformation(name) Values (@name)

行中:
cmd.Parameters.AddWithValue("name", textBox1.Text);

参数名称不应包含 @ 。

The problem here seams to be the parameter. In the command text you don't specify its name, but when you add it, it has a name. Change command text to :

Insert Into UserInformation(name) Values (@name)

In line:
cmd.Parameters.AddWithValue("name", textBox1.Text);

the parameter name should stay without @ .

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