C#簡略銜接sql數據庫的辦法。本站提示廣大學習愛好者:(C#簡略銜接sql數據庫的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#簡略銜接sql數據庫的辦法正文
本文實例講述了C#簡略銜接sql數據庫的辦法。分享給年夜家供年夜家參考,詳細以下:
using System;
using System.Collections.Generic;
using System.Text;
//數據庫操尴尬刁難象庫
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調換為你設置的sa賬戶暗碼
*/
string strConn = "Data Source=HYPER-V-WIN2003\\SQLSRV2005;Initial Catalog=Mytest;User ID=sa;Password=sa";
SqlConnection conn = null;
SqlCommand sqlCmd = null;
try
{
//創立connection對象
conn = new SqlConnection(strConn);
//翻開數據庫銜接
conn.Open();
//創立Transac Sql敕令對象
sqlCmd = conn.CreateCommand();
//創立建表語句
//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("法式停止,按隨意率性鍵加入");
Console.ReadKey();
}
}
}
願望本文所述對年夜家C#法式設計有所贊助。