By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,266 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,266 IT Pros & Developers. It's quick & easy.

sql parmeter problem

P: 26
Dear all I got the following error while I trying to create database

Incorrect syntax near '@db'.

here is the code
[code]
string query = " CREATE DATABASE '@db' ON PRIMARY (NAME = @db FILENAME ='@file') ";

SqlConnection temp = new SqlConnection(@"Data Source=.\SQLEXPRESS;Database = master;Integrated Security=True;Connect Timeout=30;User Instance=True;");
SqlCommand cmd = new SqlCommand(query,temp);

cmd.Parameters.Add("@db", textBox2.Text.ToString());
cmd.Parameters.Add("@file", file.ToString());
cmd.CommandType = CommandType.Text;

temp.Open();
cmd.ExecuteNonQuery();
temp.Close();
[\CODE]
Any idea?
Mar 18 '09 #1
Share this Question
Share on Google+
1 Reply


ck9663
Expert 2.5K+
P: 2,878
You have to build your string properly. I think this is a C/Java-based code so, it will probably look like:

Expand|Select|Wrap|Line Numbers
  1. string query = " CREATE DATABASE '+ @db + ' ON PRIMARY (NAME = ' + @db + ' FILENAME =' + @file+ ') ";
  2.  
or something like it


-- CK
Mar 18 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.