zoukankan      html  css  js  c++  java
  • C# 操作数据库表和数据库

    <1>c#创建数据库表:

            private void CreatTable(string name)      //创建数据库源数据表,name为表名

            {

                con.ConnectionString = s;

                con.Open();

                string sql = "CREATE TABLE " + name + "(Line INTEGER, FH1 nchar(8),FH2 nchar(8),";

                sql += " FH3 nchar(8),FH4 nchar(8),FH5 nchar(8),FH6 nchar(8),FH7 nchar(8))";

                sql += "primary key(Line),foreign key(Line) references 患者信息(编号))"; //设置外键约束

                SqlCommand cmd = new SqlCommand(sql, con);

                cmd.ExecuteNonQuery();

                con.Close();

            }

    <2>C# 更新数据库表

             con = new SqlConnection(s);

             con.Open();

             string sqlstr = "update 系统参数 set 内容='" + content + "' where 类型='" + type + "'" + "and 名称='" + name + "'"; ;   //单引号,然后是双引号

                cmd = new SqlCommand(sqlstr, con);

                cmd.ExecuteNonQuery();

                con.Close();

    <3>C#创建数据库代码

          string Connectionstr = "Data Source=A3135;Initial Catalog=;Integrated Security=True";

          string sqlstr = "select count(*) from sys.databases where name='病历管理系统数据库'";

          SqlConnection con = new SqlConnection();

          con.ConnectionString = Connectionstr;

          con.Open();

          SqlCommand cmd = new SqlCommand(sqlstr, con);   //判断当前数据库是否存在

          int flag = int.Parse(cmd.ExecuteScalar().ToString()); //flag>0则存在,flag<=0则不存在

          con.Close();

          if (flag <= 0)

          {

             con.Open();

             sqlstr = "create database 病历管理系统数据库 on(name=病历管理统数据库_data,"

                    + "filename='C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\病历管理系统数据库.mdf'"

                    + ",size=3,maxsize=20,filegrowth=1)log on(name=病历管理系统数据库_log,filename=

                     'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\病历管理系统数据库.ldf'"

                     + ",size=3,maxsize=20,filegrowth=1)";

              SqlCommand cmd1 = new SqlCommand(sqlstr, con);  //创建数据库

              cmd1.ExecuteNonQuery();

              con.Close();

         }

    <4>C# 查询字符串是否含有str字符

         sqlstr += " select * from TableName where 姓名 like '%" + str + "%'";

    <5>C# 获取本机局域网内所有数据库的名字:

      private List<string> GetSqlServerName()

      {

           DataTable dataSources = SqlClientFactory.Instance.CreateDataSourceEnumerator().GetDataSources();

           DataColumn column = dataSources.Columns["InstanceName"];

           DataColumn column2 = dataSources.Columns["ServerName"];

           DataRowCollection rows = dataSources.Rows;

           List<string> Serverlist = new List<string>();

           string array = string.Empty;

           for (int i = 0; i < rows.Count; i++)

           {

                string str2 = rows[i][column2] as string;

                string str = rows[i][column] as string;

                if (((str == null) || (str.Length == 0)) || (str == "MSSQLSERVER"))

                {

                     array = str2;

                } 

                else

                {

                     array = str2 + @"" + str;

                }

                Serverlist.Add(array);

           }

           Serverlist.Sort();

           return Serverlist;

       }

  • 相关阅读:
    html meta标签使用总结
    HTTP与HTTPS握手的那些事
    JS windows.open打开窗口并居中
    什么是响应式web设计
    phpstorm2017 激活方法
    php与mysql 绑定变量和预定义处理
    php和mysql数据库防SQL注入的有效解决办法
    如何高效率的写一个不会重复出现的随机数
    php将IP地址转换为真实地址的方法
    PHP程序员要掌握哪些技术
  • 原文地址:https://www.cnblogs.com/zhanglei93/p/4762081.html
Copyright © 2011-2022 走看看