您的位置:首页 > 数据库

操作数据库的几种写法:

2012-06-29 21:45 288 查看
(1) string sqlconnection = ConfigurationManager.AppSettings["CeShiConn2"];
SqlConnection mysqlconnection = new SqlConnection(sqlconnection);
string sql = "Select * from News ";
mysqlconnection.Open();
SqlCommand mycommand = new SqlCommand();
mycommand.Connection = mysqlconnection;
mycommand.CommandText = sql;
SqlDataAdapter myadapter = new SqlDataAdapter();
myadapter.SelectCommand = mycommand;
DataSet ds = new DataSet();
myadapter.Fill(ds, "NewsTable1");
this.GridView1.DataSource = ds.Tables["NewsTable1"];
this.GridView1.DataBind();

(2)

//try
//{
// using (SqlConnection mysqlconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["CeShiConn"].ToString())) //引用using好处只需将链接打开不用写关闭链接的代码就会释放资源
// {
// mysqlconnection.Open();
// string sql = "select * from News";
// SqlDataAdapter adapter = new SqlDataAdapter(sql, mysqlconnection);
// DataSet ds = new DataSet();
// adapter.Fill(ds, "NewsTable");
// this.GridView1.DataSource = ds.Tables["NewsTable"];
// this.DataBind();
// if (ds.Tables[0].Rows.Count > 0)
// {
// Response.Write("<script> window.alert('登陆成功!')</script>");
// }

// }
// this.Label1.Text = "数据库连接完毕";
//}
//catch
//{

// Response.Write("<script> window.alert('登录失败,请重新登录!')</script>");
//}

(3) ////int s = Convert.ToInt32(cmd.ExecuteScalar());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: