程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#與SQL server 的連接

C#與SQL server 的連接

編輯:C#入門知識

數據庫的服務器

 \

 

管理員信息表

 \

 

學生信息表

 \

 

一、登陸窗口

 \

 

 源代碼:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;        //此處的應用是必要的


namespace 數據庫
{
    public partial class Form1 : Form
    {  
        public Form1()
        {
            InitializeComponent();
            textBox2.PasswordChar='*';  //把密碼設為隱藏的
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox1.Focus();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == ""&&textBox2.Text=="")
            {
                MessageBox.Show("請輸入管理員的賬戶和密碼!");
                textBox1.Focus();
            }
            else
            {
                if (textBox1.Text == "" && textBox2.Text != "")
                {
                    MessageBox.Show("請輸入管理員的賬戶!");
                    textBox1.Focus();
                }
                else
                {
                    if (textBox1.Text != "" && textBox2.Text == "")
                    {
                        MessageBox.Show("請輸入密碼!");
                        textBox2.Focus();
                    }
                    else
                    {
                        SqlConnection thisConn = new SqlConnection("Server=szpt-ceacc3d660\\SQLEXPRESS;Integrated Security=true;Database=數據庫");  //創建與數據庫的連接
                        SqlDataAdapter thisAdapter = new SqlDataAdapter("select * from 管理員信息", thisConn);        //創建SQL適配器
                        SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);   //創建SQL命令生成器
                        DataSet thisDataSet = new DataSet();           //創建數據集
                        thisAdapter.Fill(thisDataSet, "Administrator"); //將查詢結果填充到數據集中
                        int i = 0;
                        foreach(DataRow row in thisDataSet.Tables["Administrator"].Rows)
                        {
                            if (textBox1.Text == row["管理員"].ToString())
                            {
                                if (textBox2.Text == row["密碼"].ToString())
                                {
                                    MessageBox.Show("登錄成功!");
                                   
                                    this.Close();
                                   
                                    break;
                                }
                                else
                                {
                                    MessageBox.Show("密碼錯誤!請重新輸入密碼!");
                                    textBox2.Text = "";
                                    textBox2.Focus();
                                }
                            }


                            else
                            {
                                i=i+1;
                                if (i == thisDataSet.Tables["Administrator"].Rows.Count)
                                {
                                    MessageBox.Show("無此管理員!請重新輸入!");
                                    textBox1.Text = "";
                                    textBox2.Text = "";
                                    textBox1.Focus();
                                }
                            }
                        }
                    }


                }
           
            }
           


        }
    }
}

 

 

二、數據庫操作窗口

 \

\

\

\

\

 

源代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;             //此處的應用是必要的

 


namespace 數據庫
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
           
        }


        private void button1_Click(object sender, EventArgs e)
        {
           
            SqlConnection thisConn = new SqlConnection("Server=szpt-ceacc3d660\\SQLEXPRESS;Integrated Security=true;Database=數據庫");  //創建與數據庫的連接
            SqlDataAdapter thisAdapter = new SqlDataAdapter("select * from 學生信息", thisConn);                                        //創建SQL適配器
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);                                                         //創建SQL命令生成器
            DataSet thisDataSet = new DataSet();                                                                                        //創建數據集
            thisAdapter.Fill(thisDataSet, "Student");                                                                                   //將查詢結果填充到數據集中


            listBox1.Items.Clear();
            listBox1.Items.Add(" 學號    姓名   性別   年齡   成績");
          foreach(DataRow row in thisDataSet.Tables["Student"].Rows)
          {
              listBox1.Items.Add(row["學號"].ToString() + "    " + row["姓名"].ToString() + "    " + row["性別"].ToString() + "     " + row["年齡"].ToString() + "     " + row["成績"].ToString());
          }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection thisConn = new SqlConnection("Server=szpt-ceacc3d660\\SQLEXPRESS;Integrated Security=true;Database=數據庫");  //創建與數據庫的連接
            SqlDataAdapter thisAdapter = new SqlDataAdapter("select * from 學生信息", thisConn);                                        //創建SQL適配器
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);                                                          //創建SQL命令生成器
            DataSet thisDataSet = new DataSet();           //創建數據集
            thisAdapter.Fill(thisDataSet, "Student"); //將查詢結果填充到數據集中


            DataRow newRow = thisDataSet.Tables["Student"].NewRow();   //創建新行
            newRow["學號"] = textBox1.Text.ToString();
            newRow["姓名"] = textBox2.Text.ToString();
            newRow["性別"] = textBox3.Text.ToString();
            newRow["年齡"] = textBox4.Text.ToString();
            newRow["成績"] = textBox5.Text.ToString();
            thisDataSet.Tables["Student"].Rows.Add(newRow);      //增加到數據集裡
            thisAdapter.Update(thisDataSet,"Student");         //寫到數據庫中
            MessageBox.Show("插入數據成功!");
            listBox2.Items.Clear();
            listBox2.Items.Add(" 學號    姓名   性別   年齡   成績");
            foreach(DataRow row in thisDataSet.Tables["Student"].Rows)
            {
            listBox2.Items.Add(row["學號"].ToString() + "    " + row["姓名"].ToString() + "    " + row["性別"].ToString() + "     " + row["年齡"].ToString() + "     " + row["成績"].ToString());
         
            }
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }


        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection thisConn = new SqlConnection("Server=szpt-ceacc3d660\\SQLEXPRESS;Integrated Security=true;Database=數據庫");  //創建與數據庫的連接
            SqlDataAdapter thisAdapter = new SqlDataAdapter("select * from 學生信息", thisConn);        //創建SQL適配器
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);   //創建SQL命令生成器
            DataSet thisDataSet = new DataSet();           //創建數據集
            thisAdapter.Fill(thisDataSet, "Student"); //將查詢結果填充到數據集中


            thisDataSet.Tables["Student"].Rows[Convert.ToInt16(textBox6.Text)].Delete();
            thisAdapter.Update(thisDataSet, "Student");


            listBox3.Items.Clear();
            listBox3.Items.Add(" 學號    姓名   性別   年齡   成績");
            foreach (DataRow row in thisDataSet.Tables["Student"].Rows)
            {
                listBox3.Items.Add(row["學號"].ToString() + "    " + row["姓名"].ToString() + "    " + row["性別"].ToString() + "     " + row["年齡"].ToString() + "     " + row["成績"].ToString());


            }
            textBox6.Text = "";
        }


        private void button4_Click(object sender, EventArgs e)
        {
            SqlConnection thisConn = new SqlConnection("Server=szpt-ceacc3d660\\SQLEXPRESS;Integrated Security=true;Database=數據庫");  //創建與數據庫的連接
            SqlDataAdapter thisAdapter = new SqlDataAdapter("select * from 學生信息", thisConn);        //創建SQL適配器
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);   //創建SQL命令生成器
            DataSet thisDataSet = new DataSet();           //創建數據集
            thisAdapter.Fill(thisDataSet, "Student"); //將查詢結果填充到數據集中


            thisDataSet.Tables["Student"].Rows[Convert.ToInt16(textBox7.Text)]["成績"] = textBox8.Text;
            thisAdapter.Update(thisDataSet, "Student");


            listBox4.Items.Clear();
            listBox4.Items.Add(" 學號    姓名   性別   年齡   成績");
            foreach (DataRow row in thisDataSet.Tables["Student"].Rows)
            {
                listBox4.Items.Add(row["學號"].ToString() + "    " + row["姓名"].ToString() + "    " + row["性別"].ToString() + "     " + row["年齡"].ToString() + "     " + row["成績"].ToString());


            }
            textBox7.Text = "";
            textBox8.Text = "";
        }
       
      


       private void button5_Click(object sender, EventArgs e)
        {
          
            string command1 ="select * from 學生信息 where 性別='男'";
            SqlConnection thisConn = new SqlConnection("Server=szpt-ceacc3d660\\SQLEXPRESS;Integrated Security=true;Database=數據庫");  //創建與數據庫的連接
            SqlDataAdapter thisAdapter = new SqlDataAdapter(command1, thisConn);        //創建SQL適配器
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);   //創建SQL命令生成器
            DataSet thisDataSet = new DataSet();           //創建數據集
            thisAdapter.Fill(thisDataSet, "Student"); //將查詢結果填充到數據集中


            
            listBox5.Items.Clear();
            listBox5.Items.Add(" 學號    姓名   性別   年齡   成績");
            foreach (DataRow row in thisDataSet.Tables["Student"].Rows)
            {
                listBox5.Items.Add(row["學號"].ToString() + "    " + row["姓名"].ToString() + "    " + row["性別"].ToString() + "     " + row["年齡"].ToString() + "     " + row["成績"].ToString());


            }
        }


        private void button6_Click(object sender, EventArgs e)
        {
            int i = 60;
            string command2 = "select * from 學生信息 where 成績<"+i;
            SqlConnection thisConn = new SqlConnection("Server=szpt-ceacc3d660\\SQLEXPRESS;Integrated Security=true;Database=數據庫");  //創建與數據庫的連接
            SqlDataAdapter thisAdapter = new SqlDataAdapter(command2, thisConn);        //創建SQL適配器
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);   //創建SQL命令生成器
            DataSet thisDataSet = new DataSet();           //創建數據集
            thisAdapter.Fill(thisDataSet, "Student"); //將查詢結果填充到數據集中

 


            listBox5.Items.Clear();
            listBox5.Items.Add(" 學號    姓名   性別   年齡   成績");
            foreach (DataRow row in thisDataSet.Tables["Student"].Rows)
            {
                listBox5.Items.Add(row["學號"].ToString() + "    " + row["姓名"].ToString() + "    " + row["性別"].ToString() + "     " + row["年齡"].ToString() + "     " + row["成績"].ToString());


            }
        }


        private void button7_Click(object sender, EventArgs e)
        {
            int age=17;
            string command3 = "select * from 學生信息 where 年齡>"+age+"and 性別='男'";
            SqlConnection thisConn = new SqlConnection("Server=szpt-ceacc3d660\\SQLEXPRESS;Integrated Security=true;Database=數據庫");  //創建與數據庫的連接
            SqlDataAdapter thisAdapter = new SqlDataAdapter(command3, thisConn);        //創建SQL適配器
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);   //創建SQL命令生成器
            DataSet thisDataSet = new DataSet();           //創建數據集
            thisAdapter.Fill(thisDataSet, "Student"); //將查詢結果填充到數據集中

 


            listBox5.Items.Clear();
            listBox5.Items.Add(" 學號    姓名   性別   年齡   成績");
            foreach (DataRow row in thisDataSet.Tables["Student"].Rows)
            {
                listBox5.Items.Add(row["學號"].ToString() + "    " + row["姓名"].ToString() + "    " + row["性別"].ToString() + "     " + row["年齡"].ToString() + "     " + row["成績"].ToString());


            }
        }


        private void button8_Click(object sender, EventArgs e)
        {
            string command4 = "select * from 學生信息 where 性別='女'";
            SqlConnection thisConn = new SqlConnection("Server=szpt-ceacc3d660\\SQLEXPRESS;Integrated Security=true;Database=數據庫");  //創建與數據庫的連接
            SqlDataAdapter thisAdapter = new SqlDataAdapter(command4, thisConn);        //創建SQL適配器
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);   //創建SQL命令生成器
            DataSet thisDataSet = new DataSet();           //創建數據集
            thisAdapter.Fill(thisDataSet, "Student"); //將查詢結果填充到數據集中

 


            listBox5.Items.Clear();
            listBox5.Items.Add(" 學號    姓名   性別   年齡   成績");
            foreach (DataRow row in thisDataSet.Tables["Student"].Rows)
            {
                listBox5.Items.Add(row["學號"].ToString() + "    " + row["姓名"].ToString() + "    " + row["性別"].ToString() + "     " + row["年齡"].ToString() + "     " + row["成績"].ToString());


            }


        }
     }
}

摘自 wyzhangchengjin123
 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved