国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#簡單連接sql數(shù)據(jù)庫的方法

2019-10-29 21:26:23
字體:
供稿:網(wǎng)友

本文實(shí)例講述了C#簡單連接sql數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:

using System;using System.Collections.Generic;using System.Text;//數(shù)據(jù)庫操作對象庫using System.Data;using System.Data.SqlClient;using worddic;namespace testDB{  class Program  {    static void Main(string[] args)    {      //char[] array = new char[2] ;      string [] a = new string[2];      FileOp words = new FileOp();      words.openfile("worddic.txt");      words.getwords(ref a);      /* Console.WriteLine("請輸入Data Source:");      string data_source = Console.ReadLine();      Console.WriteLine("請輸入Initial Catalog:");      string initial_catalog = Console.ReadLine();      Console.WriteLine("請輸入user id:");      string user_id = Console.ReadLine();      Console.WriteLine("請輸入pass word:");      string pword = Console.ReadLine();      //連接字符串      string strConn ="Data Source="+data_source+";Initial Catalog="+initial_catalog+";User ID="+user_id+";Password="+pword+"";//YourPwd替換為你設(shè)置的sa賬戶密碼      */      string strConn = "Data Source=HYPER-V-WIN2003//SQLSRV2005;Initial Catalog=Mytest;User ID=sa;Password=sa";      SqlConnection conn = null;      SqlCommand sqlCmd = null;      try      {        //創(chuàng)建connection對象        conn = new SqlConnection(strConn);        //打開數(shù)據(jù)庫連接        conn.Open();        //創(chuàng)建Transac Sql命令對象        sqlCmd = conn.CreateCommand();        //創(chuàng)建建表語句        //sqlCmd.CommandText = "create table wordlist(wrongwords varchar(30),rightwords varchar(30),sign char(20),)";        //sqlCmd.ExecuteScalar();        while (a[0] != null) {          sqlCmd.CommandText ="insert into wordlist(wrongwords,rightwords,sign) values('" + a[0] + "','" + a[1] + "',1 )";          sqlCmd.ExecuteScalar();          a[0] = null;          a[1] = null;          words.getwords(ref a);        }        words.fileclose();        Console.WriteLine();        //打印所有記錄      }      catch (SqlException e)      {        Console.WriteLine(e.Message);      }      finally      {        conn.Close();      }      Console.WriteLine("程序結(jié)束,按任意鍵退出");      Console.ReadKey();    }  }}


注:相關(guān)教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泰和县| 岗巴县| 彝良县| 聊城市| 三明市| 邯郸市| 凤凰县| 浮梁县| 宁陵县| 司法| 麻栗坡县| 东安县| 呼图壁县| 弥渡县| 化州市| 射阳县| 建水县| 新疆| 罗平县| 西林县| 贵定县| 松溪县| 南澳县| 遂平县| 什邡市| 连城县| 资讯 | 辉南县| 社旗县| 常熟市| 刚察县| 峨眉山市| 登封市| 宁海县| 扬州市| 凤城市| 沭阳县| 永平县| 武隆县| 东明县| 大名县|