PDA

View Full Version : سوال: تفاوت کد در حالت Sqlexpress و در حالت sql کامل



navid1n2000
شنبه 19 شهریور 1390, 09:04 صبح
من یه کد دارم :


public


string ConnectionString;



SqlConnection cnt = new SqlConnection();



SqlCommand cmd = new SqlCommand();



public Form1()

{

InitializeComponent();

}



private void button1_Click(object sender, EventArgs e)

{



richTextBox1.Text =

"در حال ایجاد پایگاه داده...";



string newPath = @"c:\VamDb";

System.IO.

Directory.CreateDirectory(newPath);

CreateConnectionString(

"master");



CreateDB();

createData();

createProcedure();



//createView();

richTextBox1.Clear();

richTextBox1.Text =

"پایگاه داده به صورت کامل ایجاد گردید";



}



private void DropDB()

{

cnt.ConnectionString = ConnectionString;

cmd.Connection = cnt;

cmd.CommandType =

CommandType.Text;

cmd.CommandText =

"DROP Database VamDB";

cnt.Open();

cmd.ExecuteNonQuery();

cnt.Close();

}



private void CreateConnectionString(string InitialCatalog)

{





SqlConnectionStringBuilder ConnectionBuilder = new SqlConnectionStringBuilder();

ConnectionBuilder.DataSource =

".\\SQLEXPRESS";

ConnectionBuilder.IntegratedSecurity =

true;

ConnectionBuilder.InitialCatalog = InitialCatalog;

ConnectionString = ConnectionBuilder.ConnectionString;





}



private void CreateDB()

{



cnt.ConnectionString = ConnectionString;

cmd.Connection = cnt;

cmd.CommandType =

CommandType.Text;



Assembly asm = Assembly.GetExecutingAssembly();



//StreamReader sr = new StreamReader(asm.GetManifestResourceStream("createdb.dbtext.txt"));



StreamReader sr = new StreamReader(Application.StartupPath + @"\dbtext.txt");



//StreamReader sr = new StreamReader(@"D:\My Project\Windows project\createdb\createdb\dbtext.txt");

cmd.CommandText = sr.ReadToEnd();

cnt.Open();

cmd.ExecuteNonQuery();

cnt.Close();





}



private void createData()

{



cnt.ConnectionString = ConnectionString;

cmd.Connection = cnt;

cmd.CommandType =

CommandType.Text;



Assembly asm = Assembly.GetExecutingAssembly();



StreamReader sr = new StreamReader(Application.StartupPath + @"\creatdata.txt");



//StreamReader sr = new StreamReader(@"D:\My Project\Windows project\createdb\createdb\creatdata.txt");

cmd.CommandText = sr.ReadToEnd();

cnt.Open();

cmd.ExecuteNonQuery();

cnt.Close();



}



private void createProcedure()

{



cnt.ConnectionString = ConnectionString;

cmd.Connection = cnt;

cmd.CommandType =

CommandType.Text;



Assembly asm = Assembly.GetExecutingAssembly();



//StreamReader sr = new StreamReader(asm.GetManifestResourceStream("createdb.dbtext.txt"));



StreamReader sr = new StreamReader(Application.StartupPath + @"\dbStoreprocedure.txt");



//StreamReader sr = new StreamReader(@"D:\My Project\Windows project\createdb\createdb\dbtext.txt");

cmd.CommandText = sr.ReadToEnd();

cnt.Open();

cmd.ExecuteNonQuery();

cnt.Close();





}



private void createView()

{



cnt.ConnectionString = ConnectionString;

cmd.Connection = cnt;

cmd.CommandType =

CommandType.Text;



Assembly asm = Assembly.GetExecutingAssembly();



//StreamReader sr = new StreamReader(asm.GetManifestResourceStream("createdb.dbtext.txt"));



StreamReader sr = new StreamReader(Application.StartupPath + @"\dbView.txt");



//StreamReader sr = new StreamReader(@"D:\My Project\Windows project\createdb\createdb\dbtext.txt");

cmd.CommandText = sr.ReadToEnd();

cnt.Open();

cmd.ExecuteNonQuery();

cnt.Close();





}


توی تابع
CreateConnectionString
اگه به جای دیتاسورس "." بذارم قشنگ کار می کنه ، یعنی توی ویندوزی که sql کامل نصب هستش کار می کنه ولی وقتی "SQLEXPRESS\\." می ذارم یعنی می خوام این کد رو توی محیطی اجرا کنم که sqlexpress نصب هست.اما ارور می ده که مثلا :
Incorrect syntax near the keyword 'TABLE'.
Incorrect syntax near the keyword 'TABLE'.
Incorrect syntax near the keyword 'TABLE'.
Incorrect syntax near the keyword 'TABLE'.
دلیلش چیه ؟