为什么 SQL table 名字不能以数字开头?
Why SQL table name cannot start with numeric?
我是 C#
和 SQL
的新手。
使用以下代码我正在尝试创建新的 table 并且一切顺利,但问题是:如果我命名 table 以数字开头,则使用 textBox
(123MytableName
) 它会抛出错误提示“'123MytableName' 附近的语法不正确”
using (SqlConnection MyConnection = new SqlConnection())
using (SqlCommand MySQLCommand = new SqlCommand())
{
MyConnection.ConnectionString = " Data Source=localhost; Initial Catalog=TestDB ;Integrated Security=true;Max Pool Size=1024; Pooling=true";
MyConnection.Open();
MySQLCommand.CommandText = "CREATE TABLE "+textBox1.Text+" (Col1 int, Col2 varchar(50), Col money)";
MySQLCommand.Connection = MyConnection;
MySQLCommand.ExecuteNonQuery();
}
我读了 this 但没有帮助。
任何人都知道如何修复它以便我可以创建以数字开头的 table?
谢谢
您可以使用 []
:
将 table 的名称转义
MySQLCommand.CommandText = "CREATE TABLE ["+textBox1.Text+"] (Col1 int, Col2 varchar(50), Col money)";
// Here ---------------------------------^-----------------^
我是 C#
和 SQL
的新手。
使用以下代码我正在尝试创建新的 table 并且一切顺利,但问题是:如果我命名 table 以数字开头,则使用 textBox
(123MytableName
) 它会抛出错误提示“'123MytableName' 附近的语法不正确”
using (SqlConnection MyConnection = new SqlConnection())
using (SqlCommand MySQLCommand = new SqlCommand())
{
MyConnection.ConnectionString = " Data Source=localhost; Initial Catalog=TestDB ;Integrated Security=true;Max Pool Size=1024; Pooling=true";
MyConnection.Open();
MySQLCommand.CommandText = "CREATE TABLE "+textBox1.Text+" (Col1 int, Col2 varchar(50), Col money)";
MySQLCommand.Connection = MyConnection;
MySQLCommand.ExecuteNonQuery();
}
我读了 this 但没有帮助。
任何人都知道如何修复它以便我可以创建以数字开头的 table?
谢谢
您可以使用 []
:
MySQLCommand.CommandText = "CREATE TABLE ["+textBox1.Text+"] (Col1 int, Col2 varchar(50), Col money)";
// Here ---------------------------------^-----------------^