关于ado.net的链接数据库的学习记录如下:
链接字符串如下:
SqlConnection cnn = new SqlConnection("Data Source=OEM-20120427JSK;Initial Catalog=数据库的名字;Integrated Security=True");连接数据库,在没有写熟练之前每次最好拷贝这几代码。
要注意的是写控制台程序的时候,控制台要加入下面这段代码在进行数据库的链接操作:
static void Main(string[] args)
{
stringdataDir=AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectoyr",dataDir );
}//上面的代码必须加上去
{
stringdataDir=AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectoyr",dataDir );
}//上面的代码必须加上去
下面是控制台的一个小程序,一个小代码。
SqlConnection cnn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=T-console;Integrated Security=True");
//SqlConnection cnn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttchDBF.lename=|DataDirectory|\Table-1.mdf;Integrated Security=True;User Tnstance=True");
cnn.Open();
Console.WriteLine("数据库连接成功");
Console.ReadKey();
}
SqlConnection cnn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=T-console;Integrated Security=True");
//SqlConnection cnn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttchDBF.lename=|DataDirectory|\Table-1.mdf;Integrated Security=True;User Tnstance=True");
cnn.Open();
Console.WriteLine("数据库连接成功");
Console.ReadKey();
}
默认是tostring方法,任何一个对象都有tostring方法。
SqlConnection cnn = new SqlConnection("Data Source==.\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");连接数据库
Data Source=OEM-20120427JSK\SQLEXPRESS;Initial Catalog=SQLcaogao;Integrated Security=True
Data Source=OEM-20120427JSK\SQLEXPRESS;Initial Catalog=SQLcaogao;Integrated Security=True
错误 Data Source=.\SQLEXPRESS;AttchDBF.lename=|DataDirectory|\Table-1.mdf;Integrated Security=True;User Tnstance=True