Jevon Jevon - 1 year ago 72
SQL Question

remove quotes from sql database name form c#

Create a table upon a button click based off the name entered in a textBox. Everything works fine but in the database the name of the table has single quotes around it. eg. the name jevon would be shown as 'jevon' in the database. Is there a way to eliminate the single quotes and display the string value alone?

private void button1_Click(object sender, EventArgs e)
string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
string query = "CREATE TABLE [dbo].['" + textBox1.Text + "'](" + "[Code] [varchar] (7) NOT NULL," +
"[Description] [varchar] (38) NOT NULL," + "[NDC] [varchar] (12) NULL, " +
"[Supplier Code] [varchar] (38) NOT NULL," + "[UOM] [varchar] (8) NULL," + "[Size] [varchar] (8) NULL,)";

using (SqlConnection connection = new SqlConnection(connectionString))
SqlCommand command = new SqlCommand(query, connection);

Answer Source

Just remove the single quotes, entity names should never be surrounded by quotes:

string query = "CREATE TABLE [dbo].[" + textBox1.Text + "](.....

And a note: it is bad style to use an unverified string like this, the user could type all kinds of stuff that totally destroys your database... so it looks more like an exercise than something serious anyway.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download