我试图在我的解决方案中使用数据库作为本地数据库,但我不知道如何在我的连接字符串中引用它。 通常我使用外部数据库,我像这样引用它
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();
答案 0 :(得分:4)
那么你怎么知道....谷歌的第一个结果!
SQL Express:
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;");
<强>的LocalDB:强>
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
答案 1 :(得分:0)
使用以下代码创建一个类
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;
}
并以您想要的形式进行相应的调用