必须声明表变量@Table [英] Must declare the table variable @table

查看:642
本文介绍了必须声明表变量@Table的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#和SQL的初学者,我有我要执行此SQL INSERT语句。它要求,我想插入其他变量之间的表名。

但是,当我运行这个控制台应用程序,我得到这个错误:


  

必须声明表变量@table


这是code的一部分:

  StreamReader的my_reader =的GetFile(参数);
字符串CS = formCS();
尝试
{
    使用(SqlConnection的CON =新的SqlConnection(CS))
    {
        COM的SqlCommand =新的SqlCommand(插入@table(时间,日期,PIN)值(@time,@date,@pin),CON);
        con.Open();
        Console.WriteLine(输入表的名称:);
        Console.Write(>>中);
        字符串tblname =到Console.ReadLine();
        com.Parameters.AddWithValue(@表,tblname);        串线=;
        诠释计数= 0;
        而((行= my_reader.ReadLine())!= NULL)
        {
            字典<字符串,字符串>结果= ExtractData由(线);
            com.Parameters.AddWithValue(@时间,导致[regTime]);
            com.Parameters.AddWithValue(@日,结果[regDate]);
            com.Parameters.AddWithValue(@针,结果[regPin]);
            数+ = com.ExecuteNonQuery();
            com.Parameters.Clear();        }
        Console.WriteLine(Recoreds补充说:{0},count.ToString());
        Console.WriteLine(preSS Enter键退出。);
    }
    到Console.ReadLine();
}
赶上(SQLEXCEPTION EX)
{
    Console.WriteLine(ex.Message);
}
赶上(异常前)
{
    Console.WriteLine(ex.Message);
}


解决方案

您不能做到这一点。你不能传递表名作为参数,你做的方式:

 的SqlCommand COM =新的SqlCommand(插入@table ......);
...
com.Parameters.AddWithValue(@表,tblname);

您可以这样做,而不是:

  Console.WriteLine(输入表的名称:);
Console.Write(>>中);
字符串tblname =到Console.ReadLine();字符串SQL =的String.Format(插入{0}(时间,日期,PIN)值...,tblname);COM的SqlCommand =新的SqlCommand(SQL,CON);...

I'm a beginner in C# and SQL, i have this SQL insert statement that i want to perform. It asks for the table name among the other variables that i want to insert.

But when i run this console app i get this error :

Must declare the table variable @table

This is a part of the code :

StreamReader my_reader =  getFile(args);
string CS = formCS();
try
{
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand com = new SqlCommand("insert into @table (time, date, pin) values (@time, @date, @pin)", con);                    
        con.Open();
        Console.WriteLine("Enter table name:");
        Console.Write(">> ");
        string tblname = Console.ReadLine();
        com.Parameters.AddWithValue("@table", tblname);

        string line = "";
        int count = 0;
        while ((line = my_reader.ReadLine()) != null)
        {
            Dictionary<string, string> result = extractData(line);                        
            com.Parameters.AddWithValue("@time", result["regTime"]);
            com.Parameters.AddWithValue("@date", result["regDate"]);
            com.Parameters.AddWithValue("@pin", result["regPin"]);
            count += com.ExecuteNonQuery();
            com.Parameters.Clear();                        

        }
        Console.WriteLine("Recoreds added : {0}", count.ToString());
        Console.WriteLine("Press Enter to exit.");
    }
    Console.ReadLine();
}
catch (SqlException ex)
{
    Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);                
}

解决方案

You can't do this. You can't pass the table name as a parameter the way you did:

SqlCommand com = new SqlCommand("insert into @table ...");
...
com.Parameters.AddWithValue("@table", tblname);

You can do this instead:

Console.WriteLine("Enter table name:");
Console.Write(">> ");
string tblname = Console.ReadLine();

string sql = String.Format("insert into {0} (time, date, pin) values ... ", tblname);

SqlCommand com = new SqlCommand(sql, con);                    

...

这篇关于必须声明表变量@Table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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