62,040
社区成员
发帖
与我相关
我的任务
分享
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string sql = "select * from authors";
dt = ReturnDataTable(sql);
List<Author> lt = new List<Author>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Author au = new Author();
au.AuthorID = dt.Rows[i][0].ToString();
au.AuthorLastName = dt.Rows[i][1].ToString();
au.AuthorFirstName = dt.Rows[i][2].ToString();
au.AuthorPhone = dt.Rows[i][3].ToString();
list.Add(au);
}
GridView1.DataSource = list;
GridView1.DataBind();
}
}
public static DataTable ReturnDataTable(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "数据库连接字符串";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand(cmdtext, cn);
cmd.CommandType = CommandType.Text; ;
SqlDataReader dr = null;
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
return dt;
}
public static int ExecuteNonQuery(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "链接字符串";
cn.Open();
int value;
try
{
SqlCommand cmd = new SqlCommand(cmdtext, cn);
value = cmd.ExecuteNonQuery() > 0 ? 1 : 0;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
}
return value;
}
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string sql = "select * from authors";
dt = ReturnDataTable(sql);
List<Author> lt = new List<Author>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Author au = new Author();
au.AuthorID = read[0].ToString();
au.AuthorLastName = read[1].ToString();
au.AuthorFirstName = read[2].ToString();
au.AuthorPhone = read[3].ToString();
list.Add(au);
}
GridView1.DataSource = list;
GridView1.DataBind();
}
}
public static DataTable ReturnDataTable(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "数据库连接字符串";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand(cmdtext, cn);
cmd.CommandType = CommandType.Text; ;
SqlDataReader dr = null;
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
return dt;
}