尝试填充数据时出错 Table

Error Trying To Fill Data Table

这是我的语法,但我得到一个错误

The SelectCommand property has not been initialized before calling 'Fill'.

我需要做什么才能填充数据table?

using (SqlConnection conn = new SqlConnection("Server=Test;Database=Test;Integrated Security=SSPI;"))
{
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT * FROM [dbo].[selfservice] WHERE saleID = @userid;";
command.Parameters.Add("@userid", SqlDbType.VarChar);
command.Parameters["@userid"].Value = row.Field<string>("saleID");
command.Connection = conn;                    
using (SqlDataAdapter dataadapter1 = new SqlDataAdapter()
{
    dataadapter1.Fill(dtData);
}
}

您必须在 Fill 之前指定 select SqlDataAdapter 命令,而且您在末尾缺少一个右括号

using (SqlDataAdapter dataadapter1 = new SqlDataAdapter())
{
    dataadapter1.SelectCommand=command; 
    dataadapter1.Fill(dtData);
}

请注意,您没有使用命令对象。您需要将 select 命令添加到您的适配器:

using (SqlDataAdapter dataadapter1 = new SqlDataAdapter()
{
    dataadapter1.SelectCommand = command
    dataadapter1.Fill(dtData);
}