PDA

View Full Version : سوال: یکبار دیتابیس اتچ شه؟



صباح فتحی
شنبه 30 مرداد 1389, 14:38 عصر
سلام توی برنامم نمیخام هربار توی شروعش دیتابیسم به sql expressوصل شه.یک دفعه وصل بشه کافیه ...
ممنون میشم کمک کنید

صباح فتحی
شنبه 30 مرداد 1389, 23:21 عصر
لطفا اگه کدی چیزی سراغ دارید اینجابزارید...

mo.esmp
یک شنبه 31 مرداد 1389, 00:19 صبح
برای اینکار کافیه هر بار که برنامه به SQL Express وصل میشه یک Query بگیری که آیا دیتابیست وجو داره و در صورت وجود عمل Attach انجام نشه.

SqlConnection sqlConnection = null;
SqlCommand sqlCmd = null;
public string pk = null;

private void Form1_Load(object sender, EventArgs e)
{
string connString = @"server = .\sqlexpress ;database = master; Integrated Security = true";
string cmdText = @"select * from master.dbo.sysdatabases where name='your db name'";

bool state = false;

using (sqlConnection = new SqlConnection(connString))
{
sqlConnection.Open();

using (sqlCmd = new SqlCommand(cmdText, sqlConnection))
{
SqlDataReader reader = sqlCmd.ExecuteReader();
state = reader.HasRows;
reader.Close();

}
}

if (state)
{
//Attach DB
}
}

صباح فتحی
یک شنبه 31 مرداد 1389, 11:18 صبح
مرسی از لطفتون...میشه کد اتچ هم بزارید؟

mo.esmp
یک شنبه 31 مرداد 1389, 14:48 عصر
Necessary Directives
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Sdk;

Server svr = new Server(@"localhost\sqlexpress");
StringCollection sc = new StringCollection();
string path = System.IO.Directory.GetCurrentDirectory() + @"\your db.mdf";
sc.Add(path);
svr.AttachDatabase("your db", sc);

صباح فتحی
یک شنبه 31 مرداد 1389, 19:42 عصر
راه حل دیگه ای هم وجود داره؟! من از sqlexpress استفاده میکنم توی کانکشن استرینگ خیلی اسان اتچ میشه..اونم قبوله؟