zoukankan      html  css  js  c++  java
  • [sqlite] 判断表、视图是否存在及常用C#操作语句

    1,判断表是否存在:

    SELECT name, sql FROM sqlite_master WHERE type="table" AND name = "Dom" 

    结果如下:

    2.判断视图是否存在:

    SELECT count(*) FROM sqlite_master WHERE type = "view" AND name = "myView"

    结果如下:


    type='view'判断视图.结果>0就是有这个视图 


    另附C#操作的常用代码:

        DataTable table = conn.GetSchema("TABLES");
        if (table != null && table.Rows.Count > 0)
           {
               string tableName = table.Rows[0]["TABLE_NAME"].ToString();
               DataTable schemaTable = GetReaderSchema(tableName, conn);
           }

      

        private DataTable GetReaderSchema(string tableName, SQLiteConnection connection)
        {
            DataTable schemaTable = null;
            IDbCommand cmd = new SQLiteCommand();
            cmd.CommandText = string.Format("select * from [{0}]", tableName);
            cmd.Connection = connection;
            using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly))
            {
                schemaTable = reader.GetSchemaTable();
            }
            return schemaTable;
        }
     
      
    

      

        foreach (DataRow dr in schemaTable.Rows)
        {
            ColumnInfo info = new ColumnInfo();
            info.Name = new NameElement(dr["ColumnName"].ToString());
            info.Ordinal = Convert.ToInt32(dr["ColumnOrdinal"].ToString());
            info.AllowDBNull = (bool)dr["AllowDBNull"];
            info.MaxLength = Convert.ToInt32(dr["ColumnSize"].ToString());
            info.DataTypeId = Convert.ToInt32(dr["ProviderType"].ToString());
            info.DataType = dr["DataTypeName"].ToString().Trim();
            info.AutoIncrement = (bool)dr["IsAutoIncrement"];
            info.IsPrimaryKey = (bool)dr["IsKey"];
            info.Unique = (bool)dr["IsUnique"];
            info.IsReadOnly = (bool)dr["IsReadOnly"];
            string netType = dr["DataType"].ToString();
     
            list.Add(info.Name.Name.ToString(), info);
        }
    

      

  • 相关阅读:
    ci 框架 报错级别 调整
    首页流氓广告的一种实现方法
    php中实现中文字符串的反转
    vmware 1021 错误解决 win7 64位
    isset 判断为POST信息是否为空 (笔记,持续更新)
    windows 下 ci 框架 命令行模式(cli)的使用
    ci 框架 excel 上传失败的处理
    php 日期处理(不断更新)
    svn的本地密码文件处理
    rpmdb open failed 的解决办法
  • 原文地址:https://www.cnblogs.com/huxiaolin/p/4157546.html
Copyright © 2011-2022 走看看