c# - SqlDataAdapter 填充错误

标签 c# sqldataadapter

我有两个表,User 和 UserReputation。我想用 SqlDataAdapter 获取数据。

        SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=abc.com;Integrated Security=True");
        SqlDataAdapter userDataAdapter = new SqlDataAdapter("SELECT * FROM User", conn);
        SqlDataAdapter userReputationDataAdapter = new SqlDataAdapter("SELECT * FROM UserReputation", conn);
        DataSet ds = new DataSet();
        userDataAdapter.Fill(ds, "User");
        userReputationDataAdapter.Fill(ds, "UserReputation");

我测试了连接字符串和连接。没有问题。但是我在 userDataAdapter.Fill(ds, "User");

处遇到以下错误

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Incorrect syntax near the keyword 'User'.

谢谢你的帮助

最佳答案

将您的查询更改为以下内容:

"SELECT * FROM [User]"

User 是一个关键字,如果你有 Table 命名为“User”你应该使用方括号
或者正如@Rahul 提到的,您也可以使用 "":

"SELECT * FROM \"User\""

关于c# - SqlDataAdapter 填充错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45117049/

相关文章:

c# - SqlCommandBuilder 内部连接更新

c# - 使用 SqlDataAdapter 更新数据库 View

c# - 在 SqlDataAdapter.Update() 中获取错误消息

c# - 无法在静态上下文中访问非静态字段 'lblSystemStatus'

c# Windows 窗体应用程序异常未抛出!

c# - Azure服务总线消息重新出现超时

c# - Entity Framework Core 中的动态 DbSet

c# - 使用 SqlDataAdapter 填充 DataTable 时 CommandTimeout 不起作用

c# - SqlDataAdapter关闭连接方法

c# - 什么限制了 .NET Core 2.2 中 HTTPS 的端口范围?