zoukankan      html  css  js  c++  java
  • 操作数据库的几种写法:

    (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());

  • 相关阅读:
    合并多行查询数据到一行:使用自连接、FOR XML PATH('')、STUFF或REPLACE函数
    行转列和列转行
    递归删除指定目录下所有文件及子目录
    有两个字符串数组a和b,寻找相同元素 (a和b都比较大)
    在Sql Server 数据库中找到含某个词的所有字段
    一段JavaScript程序
    事实表计数比事实表数据量大
    Reporting Service报表设计常见技巧及问题解法
    使用WebService的方式调用部署在服务器的Wcf服务
    LINQ To Entities如何实现查询 select * from tableA where id in (1,2,3,4) ?
  • 原文地址:https://www.cnblogs.com/lykbk/p/2342342abcd.html
Copyright © 2011-2022 走看看