太一、定義連接字符串,用來鏈接SQL Server
string str_con = "server=.(服務(wù)器名稱一般為 . );database=WordBook(數(shù)據(jù)庫名稱);uid=sa(服務(wù)器登錄名);pwd=123(服務(wù)器密碼)";
二、有了鏈接字符串之后,開始數(shù)據(jù)庫操作
 1、數(shù)據(jù)庫查詢
定義了一個查詢方法,用來調(diào)用:
public DataSet queryDatabase(string sql)    //sql是查詢語句
        {
            //儲存數(shù)據(jù)的工具初始化
            DataSet ds = new DataSet();
            //相當(dāng)于鏈接數(shù)據(jù)庫的一個工具類(連接字符串)
            using (SqlConnection con = new SqlConnection(str_con))
            {
                con.Open();  //打開
                //用SqlConnection工具鏈接數(shù)據(jù)庫,在通過sql查詢語句查詢結(jié)果現(xiàn)存入sql適配器
                SqlDataAdapter sda = new SqlDataAdapter(sql,con);   //(查詢語句和連接工具)
                sda.Fill(ds);    //將適配器數(shù)據(jù)存入DataSet工具中
                con.Close();   //用完關(guān)閉SqlConnection工具
                return ds;
            }
        }
在需要查詢數(shù)據(jù)庫的地方調(diào)用此方法:
private void query() {
            //查詢WordBook表中,book_key字段數(shù)值為7的那一行數(shù)據(jù)
            //string sql = "select * from Word_Book where book_key='7'";
            string sql = "select * from Word_Book ";     //查詢?nèi)?br />            DataSet ds = help.queryDatabase(sql);        //查詢到數(shù)據(jù)
            DataTable dt = ds.Tables[0];                       //把查到的數(shù)據(jù)存入數(shù)據(jù)表中
            sqlDataResult.DataSource = dt;                  //把數(shù)據(jù)賦值給gridView展示(全表)
            // string str=dt.Rows[0][1].ToString();//查找表中某一個內(nèi)容
            // MessageBox.Show(str);
        }
2、數(shù)據(jù)庫添加、刪除、修改
C#中數(shù)據(jù)庫的添加、刪除、修改用的是同斷代碼,所以定義了一個方法,用來調(diào)用:
public int changeSqlData(String sql)
        {
            using(SqlConnection con=new SqlConnection(str_con))
            {
                con.Open();
                //操作數(shù)據(jù)庫的工具SqlCommand
                SqlCommand cmd = new SqlCommand(sql, con);//(操作語句和鏈接工具)
                int i=cmd.ExecuteNonQuery();//執(zhí)行操作返回影響行數(shù)()
                con.Close();
                return i;
            }
        }
在需要操作數(shù)據(jù)庫的地方調(diào)用此方法:
①數(shù)據(jù)庫添加:
 private void btn_add_Click(object sender, EventArgs e)
        {
            //sql添加數(shù)據(jù) insert into 表名(字段,字段...) values(‘內(nèi)容’,‘內(nèi)容’...)
            string sql = "insert into Word_Book(book_word_CN,book_word_JP,book_word_Roma,book_nominal," +
                "book_gloze) values('" + book_word_CN.Text.Trim()+"','"+ book_word_JP .Text.Trim() + "','"
                + book_word_Roma .Text.Trim() + "','"+ book_nominal.Text.Trim() + "','" + book_gloze.Text.Trim() + "')";
                int i=help.changeSqlData(sql);
            if (i == 0) MessageBox.Show("添加失敗", "提示:");
            else MessageBox.Show("添加成功", "提示:");
        }
②數(shù)據(jù)庫刪除:
 private void btn_delete_Click(object sender, EventArgs e)
        {
            //根據(jù)同個字段中不同內(nèi)容刪除多行
            //delete from Word_Book where book_key in (1,2,3)
            //sql刪除數(shù)據(jù)delete 表名 where 字段='內(nèi)容'單個條件用or鏈接,多個條件用and鏈接
            string sql = "delete from Word_Book where book_key='"+book_key.Text.Trim()+"'";
            int i=help.changeSqlData(sql);
            if (i == 0) MessageBox.Show("刪除失敗", "提示:");
            else MessageBox.Show("刪除成功", "提示:");
        }
②數(shù)據(jù)庫更新:
 private void btn_update_Click(object sender, EventArgs e)
        {
            //根據(jù)條件修改多個字段內(nèi)容
            //update 表名 set 字段='內(nèi)容', 字段='內(nèi)容' where 條件字段='內(nèi)容'
            string sql = "update Word_Book set book_word_CN='"+book_word_CN.Text.Trim()+
                "', book_word_JP='"+book_word_JP.Text.Trim()+"'where book_key='" + book_key.Text.Trim()+"'";
            int i = help.changeSqlData(sql);
            if (i == 0) MessageBox.Show("修改失敗", "提示:");
            else MessageBox.Show("修改成功", "提示:");
        }