- C# code
using System;using System.Data.SqlClient;using System.Linq;using System.Data.Linq;using System.IO;class TestBoxing{ static void Main() { string userTempFolder = Environment.GetEnvironmentVariable("SystemDrive") + @"\YJingLee"; string userMDF = System.IO.Path.Combine(userTempFolder, @"NewCreateDB.mdf"); string connStr = String.Format(@"Data Source=hm;AttachDbFilename={0};User ID=sa;pwd=sa;", userMDF); DataContext db = new DataContext(connStr); //string strConn = @"Data Source=hm;Initial Catalog=Test;User ID=sa;pwd=sa"; //DataContext db = new DataContext(strConn); if (db.DatabaseExists() == true) { Console.WriteLine(db.Connection.Database + "数据库已经存在。"); } else { Console.WriteLine(db.Connection.Database + "数据库不存在。"); db.CreateDatabase(); } Console.ReadLine(); }}
问题1:当我用这样的方式的时候
userMDF="C:\YJingLee\NewCreateDB.mdf";
而这个数据库是米有的,
执行到
db.CreateDatabase()方法的时候会出现这样的错误
无法创建数据库,原因是数据上下文“DataContext”没有任何表。
这哪里的问题?
问题2:当我连接方法写成这样的时候:
string strConn = @"Data Source=hm;Initial Catalog=Test;User ID=sa;pwd=sa";
DataContext db = new DataContext(strConn);
当然也是这个问题,但是我想问的是,这两种连接字符串有什么区别?
你看的第一种,他加绝对路径干什么?
有什么用,啥意思。
你看第二种,他不加绝对路径,而且如果这个“Initial Catalog=Test”,写成在我服务器上有的数据库的话。
他会提示:数据库已经存在
说明能检测到。数据库。
--------------------
反正就是想问这么搞个连接数据库的字符串。
给我链接,或给我解惑下。Thanks。
------解决方案--------------------------------------------------------
呵呵。。。。
听LZ口气就知道在等sp1234
老哥帮你顶顶帖子吧
------解决方案--------------------------------------------------------
***到此一游!
------解决方案--------------------------------------------------------
这个是我以前写的一个例子。连接查询很简单的啊
- C# code
using System;using System.Collections.Generic;using System.Text;using System.Data.SqlClient;using System.Data;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { // "uid=sa":连接数据库的用户名为sa. // "password=":连接数据库的验证密码为空.他的别名为"pwd",所以我们可以写为"pwd=". // "initial catalog=Northwind":使用的数据源为"Northwind"这个数据库.他的别名为"Database",本句可以写成"Database=Northwind". // "Server=YourSQLServer":使用名为"YourSQLServer"的服务器.他的别名为"Data Source","Address","Addr". // " Connect Timeout=30":连接超时时间为30秒.(根据情况添加) // PS: // 1.你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用"uid"和"password"这样的方式来登录,而需要使用"Trusted_Connection=SSPI"来进行登录. // 2. 如果使用的是本地数据库且定义了实例名,则可以写为"Server=(local)\实例名";如果是远程服务器,则将"(local)"替换为远程服务器的名称或IP地址. string strConnection = "Trusted_Connection=SSPI;"; strConnection += "database=NTF_Navision_enlistment60;Server=CAIXIATA-6BE823;"; strConnection += "Connect Timeout=30"; using (SqlConnection objConnection = new SqlConnection(strConnection)) { objConnection.Open(); // method 1 SqlCommand myCommand = new SqlCommand("select * from Couse", objConnection); Console.WriteLine("open"); SqlDataReader myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Console.WriteLine(myReader.GetFieldType(0)); Console.WriteLine(myReader.GetFieldType(1)); Console.WriteLine(myReader.GetFieldType(2)); Console.WriteLine(myReader.GetDecimal(0)); Console.WriteLine(myReader.GetDecimal(1)); Console.WriteLine(myReader.GetDecimal(2)); } myReader.Close(); // method 2 SqlDataAdapter myCommandd = new SqlDataAdapter("select * from Couse", objConnection); DataSet ds = new DataSet(); myCommandd.Fill(ds, "Couse"); DataTable dt = ds.Tables["couse"]; Console.WriteLine(dt.Columns[0].ToString()); Console.WriteLine(dt.DefaultView.Count); } } }}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LCL_data/archive/2009/04/30/4139145.aspx