该段程序是:
SqlConnection conn = new SqlConnection(@"Data Source =.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
无意中自己解决了,运气真好,还是谢谢大家热心的帮助
SqlConnection conn = new SqlConnection(@"Data Source =.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
这段代码不换行就编译通过了
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
为什么换行就不行,写成一行就行了呢,真是疑惑啊,哎~~~
是不是你的解决方案路径中有中文(文件夹中有中文)?
谢谢您的回答,我留意了输入法是用的英文的。
|DataDirectory|
你真的是这样写的?还是你要表达的这里是你的文件路径?
谢谢您的回答,这个是使用的连接字符串,就是路径咧,我初学,见笑了。
所有代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace 数据库
{
class Program
{
static void Main(string[] args)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\")
|| dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
SqlConnection conn = new SqlConnection(@"Data Source
=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\
Database1.mdf;Integrated Security=True;User Instance=True");
conn.Open();
Console.WriteLine("打开数据库成功!");
Console.ReadKey();
}
}
}
然后系统提示说:SqlConnection conn = new SqlConnection(@"Data Source =.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
路径中有非法字符
希望您能帮我解惑。。。。。。
SqlConnection conn = new SqlConnection(@"Data Source
=. ;SQLEXPRESS;AttachDBFilename=|DataDirectory|\; Database1.mdf;Integrated Security=True;User Instance=True");
这样对不对啊