string sql1 = "select * from article where time="+param+"";
SqlDataAdapter sda1 = new SqlDataAdapter(sql1, conn);
DataSet ds1 = new DataSet();
sda1.Fill(ds1);
DataTable dt1 = ds1.Tables[0];
于是改成用sqlcommand,请问这个怎么能够写入datatable
SqlCommand sc = new SqlCommand("select * from article where time=@p");
sc.Connection = conn;
sc.Parameters.AddWithValue("p","'"+param+"'");
DataTable dt1 = ds1.Tables[0];
3 个解决方案
#2
DataSet ds = new DataSet();
SqlCommand sc = new SqlCommand("select * from article where time=@p");
sc.Connection = conn;
sc.Parameters.AddWithValue("@p", "'" + param + "'");
SqlDataAdapter sda = new SqlDataAdapter(sc);
sda.Fill(ds);
#3
不行,datatable中没有数据
异常详细信息: System.IndexOutOfRangeException: 在位置 0 处没有任何行。
行 36: Label1.Text = dt1.Rows[0]["title"].ToString();
#1
#2
DataSet ds = new DataSet();
SqlCommand sc = new SqlCommand("select * from article where time=@p");
sc.Connection = conn;
sc.Parameters.AddWithValue("@p", "'" + param + "'");
SqlDataAdapter sda = new SqlDataAdapter(sc);
sda.Fill(ds);
#3
不行,datatable中没有数据
异常详细信息: System.IndexOutOfRangeException: 在位置 0 处没有任何行。
行 36: Label1.Text = dt1.Rows[0]["title"].ToString();