我正在尝试从数据库中进行选择
Db db = new Db();
var con = await db.OpenConnection();
SqlCommand command = new SqlCommand("SELECT * FROM users", con);
SqlDataReader reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
...
}
await db.CloseConnection(con);
但我有一个例外
Microsoft.Data.SqlClient.SqlException (0x80131904): 对象名称“users”无效。
我不知道发生了什么,因为数据库存在,表也存在
课程代码
using System.Data;
using Microsoft.Data.SqlClient;
namespace CarWashServer
{
internal class Db
{
public async Task<SqlConnection> OpenConnection()
{
var con = new SqlConnection();
con.ConnectionString = "Data Source=localhost;User ID=*****;Password=********;Connect Timeout=30;Encrypt=False;Trust Server Certificate=True;Application Intent=ReadWrite;Multi Subnet Failover=False";
if (con.State != ConnectionState.Open)
await con.OpenAsync();
return con;
}
public async Task<int> CloseConnection(SqlConnection con)
{
await con.CloseAsync();
return 0;
}
}
}
祝你好运!