如何在OleDB中将表名作为参数传递? [英] How to pass table name as parameter in OleDB?

查看:60
本文介绍了如何在OleDB中将表名作为参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
{
        string tablename = label2.Text;
        string name = TextBox1.Text;
        DBconnection.savetodb(tablename, name);           

}

我从另一种形式调用下面的方法将名称保存到特定表中.但它不会保存到我的数据库表中.

I call the method below from another form to save the name into a specific table. But it wont save into my table in database.

public static void savetodb(string tablename, string name)
{
        OleDbConnection connection = GetConnection();
        string query = String.Format("INSERT INTO {0} (Name) VALUES (@Name)", tablename);

        OleDbCommand cmd = new OleDbCommand(query, connection);
        cmd.Parameters.AddWithValue("@Name", name);

        try{
            connection.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex){
            Console.WriteLine("Exception catch", ex);
        }
        finally{
            myConnection.Close();
        }

感谢您的帮助.

推荐答案

您不是将表名作为参数传递,而是将您的 @Name 值作为参数传递.如果需要,您不能将表名作为参数传递甚至.参数仅用于值,而不是表或列名称.您只是在格式化基于查询的表名.据我所知,您使用命名参数的问题.OleDb 提供程序不支持命名参数.

You are not passing table name as a parameter, you are passing your @Name value as a parameter. You can't pass a table name as a parameter even if you want. Parameters only for values, not table or column names. You are just formatting your query based table name. As far as I see, your problem using named parameters. OleDb provider does not support named parameters.

来自 OleDbCommand.Parameters

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

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 = ?

SELECT * FROM Customers WHERE CustomerID = ?

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

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.

试试看;

string query = String.Format("INSERT INTO {0} (Name) VALUES (?)", tablename);
...
cmd.Parameters.AddWithValue("@name", name);

还使用 using 语句处理您的 OleDbConnectionOleDbCommand.

Also use using statement to dispose your OleDbConnection and OleDbCommand.

using(OleDbConnection connection = new GetConnection())
using(OleDbCommand cmd = con.CreateCommand())
{

}

并考虑使用 .Add 方法 代替 .AddWithValue.它可能会导致一些问题.阅读 我们可以停止使用 AddWithValue() 已经?

And consider to use .Add method instead .AddWithValue. It may cause some problems. Read Can we stop using AddWithValue() already?

这篇关于如何在OleDB中将表名作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆