-1

我正在尝试在我的解决方案中使用数据库作为本地数据库,但我不知道如何在我的连接字符串中引用它。通常我使用外部数据库,我像这样引用它

SqlConnection miCon = new SqlConnection(@"Data S...")
miCon.Open();
SqlDataAdapter miDa = new SqlDataAdapter("select distinct tipo from     ejercicios", miCon);
DataSet miDs = new DataSet();
miDa.Fill(miDs);

foreach (DataRow row in miDs.Tables[0].Rows)
{
    comboBox1.Items.Add(row[0].ToString());
}          


miCon.Close();
4

2 回答 2

4

那么你怎么知道......谷歌第一个结果!

SQL 快递:

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;

var con = new SqlConnection(@"Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;");

本地数据库:

Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\mydbfile.mdf;Integrated Security=True

var con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\mydbfile.mdf;Integrated Security=True");

http://www.connectionstrings.com/sql-server-2008
http://www.connectionstrings.com/sql-server-2012

于 2013-08-29T08:52:50.133 回答
0

create a class with the following code

private static readonly SqlConnection SqlConnection = new SqlConnection("Data Source=server name;Initial Catalog=database name;Persist Security Info=True;User ID= ;Password= ");`

public SqlConnection ServerConnection()
{
    if (SqlConnection.State == ConnectionState.Open)
    {
        SqlConnection.Close();
    }
    else
    {
        SqlConnection.Open();
    }
    return SqlConnection;

}

and the call accordingly in the forms you want

于 2013-08-29T09:23:27.347 回答