操作数据库的几种写法:
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());
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());
相关文章推荐
- 对数据库的几种基本操作
- Hibernate操作数据库占位符写法
- Codeigniter操作数据库表的优化写法总结
- Entity Framework 实体框架的形成之旅--几种数据库操作的代码介绍(9)
- 几种常用数据库的driver、url写法
- ADO中数据库连接字符串的几种写法
- zend framework 中使用dbadapter操作数据库的几种方法(持续更新)
- 几种常用数据库(SQLServer Oracle db2 mysql等)分页SQL的写法总结
- 几种常见的数据库连接的URL写法
- asp.net下ADO.NET操作数据库的几种方式总结
- Codeigniter操作数据库表的优化写法总结
- CI框架教程6——操作数据库表的优化写法
- Java数据库的操作——DBUtils工具类结果集处理的方式有几种?
- 盘点几种数据库的分页SQL的写法
- 数据工厂模式,多种数据库选择 web.config 写法及相关操作
- 20080905经典的DataReader对象解读+几种对数据库的操作的命令,20100111update read db example
- 关于OleDbCommand中操作数据库的几种方法的区别
- JDBC操作数据库的几种方式。
- 使用ODBC操作数据库的几种方法
- Yii框架操作数据库的几种方式与mysql_escape_string