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;

   }

posted @ 2015-08-27 00:12  best.lei  阅读(1637)  评论(0编辑  收藏  举报