如何将数据库 Table 名称与文本框进行比较?
How Can Compare Database Table Name With Textbox?
我正在使用 C# 在 visual studio 中创建 table 按钮。但是我在创建 table 函数时遇到问题。如果我在创建 table 时使用相同的 table 名称,则会出现错误。如何将我的文本框与数据库 table 名称进行比较?
if(txtbox_loladı.Text=)
{
con.Open();
SqlCommand Cmd = new SqlCommand();
string Sql = "CREATE TABLE " + txtbox_loladı.Text + " (No nvarchar(50), [Name Surname] nvarchar(50), PRIMARY KEY (No))";
Cmd = new SqlCommand(Sql, con);
SqlDataReader SQLDR = Cmd.ExecuteReader();
MessageBox.Show("Table Has Been Created!");
}
else
{
MessageBox.Show("You Need Change Table Name");
}
不要那样做。使用 create table
if not exists
命令代替
编辑:
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='tbl_name' and xtype='U')
CREATE TABLE tbl_name (
Name varchar(64) not null
)
GO
使用您的代码:
string Sql = "IF NOT EXISTS SELECT * FROM sysobjects WHERE name='" + txtbox_loladı.Text + "' and xtype='U') "
Sql += "CREATE TABLE " + txtbox_loladı.Text + " (No nvarchar(50), [Name Surname] nvarchar(50), PRIMARY KEY (No))";
// I don't know whether you need "Go" as well
你应该使用 $" {...} ... {...}"
而不是 "..." + .. + "..."
我正在使用 C# 在 visual studio 中创建 table 按钮。但是我在创建 table 函数时遇到问题。如果我在创建 table 时使用相同的 table 名称,则会出现错误。如何将我的文本框与数据库 table 名称进行比较?
if(txtbox_loladı.Text=)
{
con.Open();
SqlCommand Cmd = new SqlCommand();
string Sql = "CREATE TABLE " + txtbox_loladı.Text + " (No nvarchar(50), [Name Surname] nvarchar(50), PRIMARY KEY (No))";
Cmd = new SqlCommand(Sql, con);
SqlDataReader SQLDR = Cmd.ExecuteReader();
MessageBox.Show("Table Has Been Created!");
}
else
{
MessageBox.Show("You Need Change Table Name");
}
不要那样做。使用 create table
if not exists
命令代替
编辑:
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='tbl_name' and xtype='U')
CREATE TABLE tbl_name (
Name varchar(64) not null
)
GO
使用您的代码:
string Sql = "IF NOT EXISTS SELECT * FROM sysobjects WHERE name='" + txtbox_loladı.Text + "' and xtype='U') "
Sql += "CREATE TABLE " + txtbox_loladı.Text + " (No nvarchar(50), [Name Surname] nvarchar(50), PRIMARY KEY (No))";
// I don't know whether you need "Go" as well
你应该使用 $" {...} ... {...}"
而不是 "..." + .. + "..."