DataTable ,使用详细。
public static int CommandBySql(string sql)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
int flag = cmd.ExecuteNonQuery();
conn.Close();
return flag;
}
public static DataSet GetDsBySql(string sql)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
int flag = OperaterSqlBase.CommandBySql("update users set UserName='" + name + "',Pwd='" + pwd + "' where UserId=" + id + "");
使用:GetDsBySql
DataSet ds = OperaterSqlBase.GetDsBySql("select * from dbo.Users where userid=" + id + "");
if (ds.Tables[0].Rows.Count > 0)
{
txtName.Text = ds.Tables[0].Rows[0]["UserName"].ToString
txtPwd.Text = ds.Tables[0].Rows[0]["Pwd"].ToString();
}
第二中数据库连接:
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("data source=.;initial catalog=FIIP_ADM;user id=sa;password=sa");
string sql = "select * from ADM_USER"; //存储过程名称
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}