我错过了数据库的这些错误
I missed these mistakes with database
我错过了这些错误:
System.ArgumentException: Keyword not supported: 'initial catalog'.
at System.Data.SqlServerCe.SqlCeConnectionStringBuilder.GetIndex(String keyword)
at System.Data.SqlServerCe.SqlCeConnectionStringBuilder.set_Item(String keyword, Object value)
at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
at System.Data.SqlServerCe.SqlCeConnectionStringBuilder..ctor(String connectionString)
at System.Data.SqlServerCe.SqlCeConnection.set_ConnectionString(String value)
at System.Data.SqlServerCe.SqlCeConnection..ctor(String connectionString)
at Task5.Data.SqlRepository`1.GetTable(String where) in D:\Task5\Task5.Data\Repositories\SqlRepository.cs:line 96
at Task5.Data.SqlRepository`1.Select() in D:\Task5\Task5.Data\Repositories\SqlRepository.cs:line 39
at Task5.Data.Services.EntityDataService.CategoryList() in D:\Task5\Task5.Data\Services\EntityDataService.cs:line 32
at Task5.Program.Step1() in D:\Task5\Task5\Program.cs:line 74
我的代码哪里有错误:
private List<TEntity> GetTable(string where = null)
{
var query = $"select * from {GetTableName()} {where}";
var dataTable = new DataTable();
using(var sqlConnection = new SqlCeConnection(connectionString))
{
SqlCeDataAdapter sqlDA = new SqlCeDataAdapter(query, sqlConnection);
sqlDA.Fill(dataTable);
}
var entityList = new List<TEntity>();
foreach (DataRow row in dataTable.Rows)
{
var entity = new TEntity();
foreach (DataColumn column in dataTable.Columns)
{
var value = row[column.ColumnName];
typeof(TEntity).GetProperty(column.ColumnName).SetValue(entity, value);
}
entityList.Add(entity);
}
return entityList;
}
正如@Amy 指出的那样,错误消息非常简单明了。这看起来像是您的连接字符串有问题,因为您可能试图将 SQL 服务器连接字符串与 SQL CE 数据库一起使用。我相信格式应该是这样的:
Data Source=MyData.sdf;Persist Security Info=False;
有关 SQL CE 连接字符串的更多详细信息,请参阅 this link。
编辑
如果您实际上正在使用 SQL 数据库(Express 或其他方式),那么您使用了错误的库来连接它。您使用的 SqlCeConnection
不支持 SQL Express 连接字符串。你应该使用 SqlConnection
和 SqlDataAdapter
.
注意:我在没有测试的情况下编辑了这个
private List<TEntity> GetTable(string where = null)
{
var query = $"select * from {GetTableName()} {where}";
var dataTable = new DataTable();
using(var sqlConnection = new SqlConnection(connectionString))
{
SqlDataAdapter sqlDA = new SqlDataAdapter(query, sqlConnection);
sqlDA.Fill(dataTable);
}
var entityList = new List<TEntity>();
foreach (DataRow row in dataTable.Rows)
{
var entity = new TEntity();
foreach (DataColumn column in dataTable.Columns)
{
var value = row[column.ColumnName];
typeof(TEntity).GetProperty(column.ColumnName).SetValue(entity, value);
}
entityList.Add(entity);
}
return entityList;
}
我错过了这些错误:
System.ArgumentException: Keyword not supported: 'initial catalog'.
at System.Data.SqlServerCe.SqlCeConnectionStringBuilder.GetIndex(String keyword)
at System.Data.SqlServerCe.SqlCeConnectionStringBuilder.set_Item(String keyword, Object value)
at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
at System.Data.SqlServerCe.SqlCeConnectionStringBuilder..ctor(String connectionString)
at System.Data.SqlServerCe.SqlCeConnection.set_ConnectionString(String value)
at System.Data.SqlServerCe.SqlCeConnection..ctor(String connectionString)
at Task5.Data.SqlRepository`1.GetTable(String where) in D:\Task5\Task5.Data\Repositories\SqlRepository.cs:line 96
at Task5.Data.SqlRepository`1.Select() in D:\Task5\Task5.Data\Repositories\SqlRepository.cs:line 39
at Task5.Data.Services.EntityDataService.CategoryList() in D:\Task5\Task5.Data\Services\EntityDataService.cs:line 32
at Task5.Program.Step1() in D:\Task5\Task5\Program.cs:line 74
我的代码哪里有错误:
private List<TEntity> GetTable(string where = null)
{
var query = $"select * from {GetTableName()} {where}";
var dataTable = new DataTable();
using(var sqlConnection = new SqlCeConnection(connectionString))
{
SqlCeDataAdapter sqlDA = new SqlCeDataAdapter(query, sqlConnection);
sqlDA.Fill(dataTable);
}
var entityList = new List<TEntity>();
foreach (DataRow row in dataTable.Rows)
{
var entity = new TEntity();
foreach (DataColumn column in dataTable.Columns)
{
var value = row[column.ColumnName];
typeof(TEntity).GetProperty(column.ColumnName).SetValue(entity, value);
}
entityList.Add(entity);
}
return entityList;
}
正如@Amy 指出的那样,错误消息非常简单明了。这看起来像是您的连接字符串有问题,因为您可能试图将 SQL 服务器连接字符串与 SQL CE 数据库一起使用。我相信格式应该是这样的:
Data Source=MyData.sdf;Persist Security Info=False;
有关 SQL CE 连接字符串的更多详细信息,请参阅 this link。
编辑
如果您实际上正在使用 SQL 数据库(Express 或其他方式),那么您使用了错误的库来连接它。您使用的 SqlCeConnection
不支持 SQL Express 连接字符串。你应该使用 SqlConnection
和 SqlDataAdapter
.
注意:我在没有测试的情况下编辑了这个
private List<TEntity> GetTable(string where = null)
{
var query = $"select * from {GetTableName()} {where}";
var dataTable = new DataTable();
using(var sqlConnection = new SqlConnection(connectionString))
{
SqlDataAdapter sqlDA = new SqlDataAdapter(query, sqlConnection);
sqlDA.Fill(dataTable);
}
var entityList = new List<TEntity>();
foreach (DataRow row in dataTable.Rows)
{
var entity = new TEntity();
foreach (DataColumn column in dataTable.Columns)
{
var value = row[column.ColumnName];
typeof(TEntity).GetProperty(column.ColumnName).SetValue(entity, value);
}
entityList.Add(entity);
}
return entityList;
}