2个回答
2014-01-06
展开全部
SqlDataAdaper(数据适配器)主要是配合DataSet(数据集)使用的
示例1.
/// <summary>
/// 获取所有管理员信息
/// </summary>
public static IList<Admin> GetAdminAll()
{
IList<Admin> adminList = new List<Admin>();
using (SqlConnection connection = new SqlConnection(connString))
{
SqlDataAdapter adapter = new SqlDataAdapter(dbOwner + ".usp_SelectAdminsAll", connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count == 0) return null;
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Admin admin = new Admin();
admin.UserId = Convert.ToInt32(row["AdminId"]);
admin.LoginId = row["LoginId"].ToString();
admin.LoginPwd = row["loginPwd"].ToString();
admin.Sex = row["sex"].ToString();
admin.UserName = row["AdminName"].ToString();
adminList.Add(admin);
}
}
return adminList;
}
示例2:
/// <summary>
/// 根据登录ID获取单个管理员信息
/// </summary>
/// <param name="loginId"></param>
/// <returns></returns>
public static Admin GetAdminByLoginId(string loginId)
{
Admin admin = new Admin();
using (SqlConnection connection = new SqlConnection(connString))
{
string sql = string.Format("select * From admin where loginid='{0}'", loginId);
SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count == 0)
{
admin = null;
}
else
{
foreach (DataRow row in dataSet.Tables[0].Rows)
{
admin.LoginPwd = row["LoginPwd"].ToString();
admin.Sex = row["sex"].ToString();
admin.UserName = row["AdminName"].ToString();
}
}
}
return admin;
}
示例1.
/// <summary>
/// 获取所有管理员信息
/// </summary>
public static IList<Admin> GetAdminAll()
{
IList<Admin> adminList = new List<Admin>();
using (SqlConnection connection = new SqlConnection(connString))
{
SqlDataAdapter adapter = new SqlDataAdapter(dbOwner + ".usp_SelectAdminsAll", connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count == 0) return null;
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Admin admin = new Admin();
admin.UserId = Convert.ToInt32(row["AdminId"]);
admin.LoginId = row["LoginId"].ToString();
admin.LoginPwd = row["loginPwd"].ToString();
admin.Sex = row["sex"].ToString();
admin.UserName = row["AdminName"].ToString();
adminList.Add(admin);
}
}
return adminList;
}
示例2:
/// <summary>
/// 根据登录ID获取单个管理员信息
/// </summary>
/// <param name="loginId"></param>
/// <returns></returns>
public static Admin GetAdminByLoginId(string loginId)
{
Admin admin = new Admin();
using (SqlConnection connection = new SqlConnection(connString))
{
string sql = string.Format("select * From admin where loginid='{0}'", loginId);
SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count == 0)
{
admin = null;
}
else
{
foreach (DataRow row in dataSet.Tables[0].Rows)
{
admin.LoginPwd = row["LoginPwd"].ToString();
admin.Sex = row["sex"].ToString();
admin.UserName = row["AdminName"].ToString();
}
}
}
return admin;
}
2014-01-06
展开全部
创建一个Dataset set =new Dataset(); //Dataset 对象
SqlDataAdapter ada; //对象
ada = new SqlDataAdapter(sql, Dbhelp.con); //Dbhelp.con是连接字符串对象
sql是SQL语句
ada.Fill(set, "biao");//SqlDataAdapter对象的填充(Fill)方法
set.Tables[0].Clear();
ada.Fill(set, "biao");
dataGridView1.DataSource = set.Tables[0]; //dataGridView里填充数据
SqlDataAdapter ada; //对象
ada = new SqlDataAdapter(sql, Dbhelp.con); //Dbhelp.con是连接字符串对象
sql是SQL语句
ada.Fill(set, "biao");//SqlDataAdapter对象的填充(Fill)方法
set.Tables[0].Clear();
ada.Fill(set, "biao");
dataGridView1.DataSource = set.Tables[0]; //dataGridView里填充数据
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询