zoukankan      html  css  js  c++  java
  • C# SQL 常用的语句

    连接字条串: "server = xxx.gotoip.net,1435;database = theTable;uid = xxxxx;pwd = xxxxx"
    选择 : select * from theTable
    select xxx,xxx,xxx, from theTable where xx = 'xxxx' or xx like '%xx%'
    增加 : Insert into theTable (xxx,xxx,xxx) values (xxx,xxx,xxx)
    修改 : update theTabel set xxx = 'xxx',xxx = 'xxx' where id = 'xxx'
    删除 : delete from theTable where id = 'xxx'

    SqlCommand 的方法: ExecuteNonQuery() : 返回的是这条命令所影响的行数 >0 说明运行成功。
                                     ExecuteReader() :返回的是一个 SqlDataReader 对象,用来读取返回的数据。

    使用 Sqlparameter: 这个东西是为了防止SQL注入,也可以写入二进制。

    sqlCommand com = new sqlCommand()
    com.CommandText = "update theTable set name = @name,age = @age"
    com.Parameters.AddRange(new SqlParameter[] {new SqlParameter("@name","xxx"),new SqlParameter("@age","34")})
    com.ExecuteNonQuery()


    SqlDataReader 的用法 : bool HasRows : 表示是否含有数据
                                     int VisibleFieldCount : 表示数据中的可见字段
                                     string GetName(int i) : 得到第i个字段的名字
                                     object GetValue(int i): 得到当前行,第i列的值
                                     bool Read() : 相当于 Next() 方法,表示是否还有数据行。一般这样用

                                     while(reader.Read()) {
                                         reader["name"].ToString(); // 得到这个行的 "name" 列的值.
                                     }

    使用 SqlDataAdapter : 一般用在 GUI 程序中,从数据库得到 DataSet(数据库在内存中的镜像).然后将DataSet 绑定到一个 控件上.
                                     SqlCommand com = new SqlCommand("xxxxx",connnection);
                                     DataSet ds = new DataSet; // 创建一个DataSet
                                     SqlDataAdapter da = new SqlDataAdapter(com); // Adapter
                                     da.Fill(ds); // 将得到的数据,填充给 DataSet
                                     DataGridView1.DataSource = ds; // 将这个DataSet 做为一个 DataGridView 的数据源.
                                     DataGridView1.DataBind(); // 绑定

  • 相关阅读:
    判断闰年
    CaesarCode
    substring
    configure: error: Cannot use an external APR with the bundled APR-util
    字符串处理487-3279
    git分支管理
    git解决冲突
    git 分支的创建和切换
    nginx与php-fpm原理
    git 远程仓库与本地项目关联
  • 原文地址:https://www.cnblogs.com/easyfrog/p/2773146.html
Copyright © 2011-2022 走看看