这是我的语法,但是我收到错误
在调用'Fill'之前,尚未初始化SelectCommand属性。
为了能够填充数据表,我需要做什么?
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);
}
}
答案 0 :(得分:1)
请注意,您没有使用命令对象。您需要将select命令添加到适配器:
using (SqlDataAdapter dataadapter1 = new SqlDataAdapter()
{
dataadapter1.SelectCommand = command
dataadapter1.Fill(dtData);
}
答案 1 :(得分:0)
你必须在Fill之前指定SqlDataAdapter的select命令,你也会在结尾处缺少一个右括号
using (SqlDataAdapter dataadapter1 = new SqlDataAdapter())
{
dataadapter1.SelectCommand=command;
dataadapter1.Fill(dtData);
}