SQL查询到的数据放到DataSet中

时间:2023-03-08 16:46:35

public DataTable GetTable(string Id)
{
StringBuilder strSql = new StringBuilder();
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection("server=localhost;database=MyData;uid=sa;pwd=123"))
{
conn.Open();
strSql.Append("SELECT * FROM PackageInfo ");

SqlCommand cmd = new SqlCommand();
if (Id != "0")
{
strSql.Append(" WHERE Id=@Id");
cmd.Parameters.Add("@Id", SqlDbType.Int);
cmd.Parameters["@Id"].Value = Id;
}
cmd.CommandText = strSql.ToString();
cmd.Connection = conn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
conn.Close();
}
return ds.Tables[0];

}