C# 连接字符串中不支持关键字:'database'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16575357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keyword not supported in Connection String: 'database'
提问by adam
Very new to C# and VS2012.
C# 和 VS2012 非常新。
I'm trying to connect to a local database connection.
我正在尝试连接到本地数据库连接。
Here is the code
这是代码
string selectSql = "select * from Tasks";
string connectionString = "Data Source=adamssqlserver;database=master;Integrated Security=true;";
using (var cn = new SqlCeConnection(connectionString))
using (var cmd = new SqlCeCommand(selectSql, cn))
{
cn.Open();
using (var reader = cmd.ExecuteReader())
{
//do something
}
}
Here is the error
这是错误
Keyword not supported: 'database'.
不支持的关键字:'database'。
If I put in Initial Catalog first
如果我先放入初始目录
"Data Source=adamssqlserver;Initial Catalog=etc;"
“数据源=adamssqlserver;初始目录=etc;”
Then the error gives the same message but for "Initial Catalog".
然后错误给出了相同的消息,但对于“初始目录”。
Here is my data connection
这是我的数据连接


采纳答案by Steve
You are using SqlCeConnectionnot a SqlConnection
您使用的SqlCeConnection不是SqlConnection
This class (SqlCeConnection) is for Sql Compact Editionwhere the syntax rules of the connection string are different. For example:
此类(SqlCeConnection)用于Sql Compact Edition连接字符串的语法规则不同的地方。例如:
Data Source=MyData.sdf;Persist Security Info=False;
Instead your connection string is for a Sql Serveror Sql Server Express.
So, if your target database is a SqlServer db as your tag indicates then you need to use
相反,您的连接字符串用于 aSql Server或Sql Server Express。因此,如果您的目标数据库是 SqlServer db,如您的标签所示,那么您需要使用
using (var cn = new SqlConnection(connectionString))
using (var cmd = new SqlCommand(selectSql, cn))
{
....
}
回答by Kate
Instead of Data Source, try Server, e.g:
而不是数据源,尝试服务器,例如:
string connectionString = "Server=adamssqlserver;Database=master";
回答by Matthew
回答by Pete855217
Just a reminder: when using MS Access databases, you need to use OleDbConnection and OleDbCommand, not SqlConnection and SqlCommand. 'Provider' in the connection string for SqlConnection is invalid AFAIK.
提醒一下:使用 MS Access 数据库时,您需要使用 OleDbConnection 和 OleDbCommand,而不是 SqlConnection 和 SqlCommand。SqlConnection 的连接字符串中的“Provider”是无效的 AFAIK。
回答by user1089766
Are you missing the " from the connection string section.
您是否缺少连接字符串部分中的“。
Should be
应该
<add name="StevenTestEntities"
connectionString="metadata=res://*/Model.TestModel.csdl|res://*/Model.TestModel.ssdl|res://*/Model.TestModel.msl;
provider=System.Data.SqlClient;
provider connection string="Data Source=Data Source=D000097;
Initial Catalog=StevenTest;
Integrated Security=True;MultipleActiveResultSets=True&qout;"
providerName="System.Data.EntityClient" />
Try that out.
试试看。

