
第一步導(dǎo)入兩個(gè)命名空間:
using.system.data
using.system.data.sqlclient


static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? //定義數(shù)據(jù)庫連接字符串
? ? ? ? ? ? string connstring="server =.; database = huawei;Integrated security = true";
? ? ? ? ? ? //創(chuàng)建連接對(duì)象
? ? ? ? ? ? SqlConnection employee = new SqlConnection(connstring);
? ? ? ? ? ? Console.WriteLine("姓名和性別");
? ? ? ? ? ? string name = Console.ReadLine();
? ? ? ? ? ? string gender = Console.ReadLine();
? ? ? ? ? ? //打開數(shù)據(jù)庫連接
? ? ? ? ? ? employee.Open();
? ? ? ? ? ? string sql;
? ? ? ? ? ? //創(chuàng)建添加的SQL語句
? ? ? ? ? ? sql = string.Format("insert Employee(name,Gender) " +
? ? ? ? ? ? ? ? "? values ('{0} ','{1}') ", name, gender);
? ? ? ? ? ? //建立command對(duì)象
? ? ? ? ? ? SqlCommand cmd = new SqlCommand(sql, employee); //兩個(gè)參數(shù)分別為 SQL語句和連接對(duì)象
? ? ? ? ? ? if (cmd.ExecuteNonQuery()>0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("已打開");
? ? ? ? ? ? }
? ? ? ? ? ? else Console.WriteLine("未打開");
? ? ? ? ? ? //執(zhí)行SQL命令,返回DataReader對(duì)象
? ? ? ? ? ? SqlDataReader reader = cmd.ExecuteReader();
? ? ? ? ? ? while (reader.Read())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string name1 = reader["name"].ToString();
? ? ? ? ? ? ? ? Console.WriteLine(name1);
? ? ? ? ? ? }
? ? ? ? ? ? //關(guān)閉Date.reader對(duì)象
? ? ? ? ? ? reader.Close();
? ? ? ? ? ? employee.Close();
? ? ? ? }



DBhelper類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;//-----5
using System.Data.SqlClient;//-----5
namespace ConsoleApplication1
{
? ? class DBHelper
? ? {
? ? ? ? //數(shù)據(jù)庫連接字符串----5
? ? ? ? public static string ConnString = "server=.;database=huawei;" +
? ? ? ? ? ? "Integrated Security=True;";
? ? ? ? //數(shù)據(jù)庫連接對(duì)象-----5
? ? ? ? public static SqlConnection Conn = null;
? ? ? ? //初始化數(shù)據(jù)庫連接---5
? ? ? ? public static void InitConnection()
? ? ? ? {
? ? ? ? ? ? //如果連接對(duì)象不存在,則創(chuàng)建連接-----5
? ? ? ? ? ? if (Conn == null)
? ? ? ? ? ? ? ? Conn = new SqlConnection(ConnString);
? ? ? ? ? ? //如果連接對(duì)象關(guān)閉,則打開連接----5
? ? ? ? ? ? if (Conn.State == ConnectionState.Closed)
? ? ? ? ? ? ? ? Conn.Open();
? ? ? ? ? ? //如果連接中斷,則重啟連接------5
? ? ? ? ? ? if (Conn.State == ConnectionState.Broken)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Conn.Close();
? ? ? ? ? ? ? ? Conn.Open();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //查詢,獲取DataReader-----總分30
? ? ? ? public static SqlDataReader GetDataReader(string sqlStr)//-----10
? ? ? ? {
? ? ? ? ? ? InitConnection();//-----4
? ? ? ? ? ? SqlCommand cmd = new SqlCommand(sqlStr, Conn);//-----4
? ? ? ? ? ? //CommandBehavior.CloseConnection 命令行為:當(dāng)DataReader對(duì)象被關(guān)閉時(shí),自動(dòng)關(guān)閉占用的連接對(duì)象
? ? ? ? ? ? return cmd.ExecuteReader(CommandBehavior.CloseConnection);//-----12
? ? ? ? }
? ? ? ? //查詢,獲取DataTable
? ? ? ? public static DataTable GetDataTable(string sqlStr)
? ? ? ? {
? ? ? ? ? ? InitConnection();
? ? ? ? ? ? DataTable table = new DataTable();
? ? ? ? ? ? SqlDataAdapter dap = new SqlDataAdapter(sqlStr, Conn);
? ? ? ? ? ? dap.Fill(table);
? ? ? ? ? ? Conn.Close();
? ? ? ? ? ? return table;
? ? ? ? }
? ? ? ? //增刪改-----總分30
? ? ? ? public static bool ExecuteNonQuery(string sqlStr)//-----10
? ? ? ? {
? ? ? ? ? ? InitConnection();//-----4
? ? ? ? ? ? SqlCommand cmd = new SqlCommand(sqlStr, Conn);//-----4
? ? ? ? ? ? int result = cmd.ExecuteNonQuery();//-----4
? ? ? ? ? ? Conn.Close();//-----4
? ? ? ? ? ? return result > 0;//-----4
? ? ? ? }
? ? ? ? //執(zhí)行集合函數(shù)-----總分30
? ? ? ? public static object ExecuteScalar(string sqlStr)//-----10
? ? ? ? {
? ? ? ? ? ? InitConnection();//-----4
? ? ? ? ? ? SqlCommand cmd = new SqlCommand(sqlStr, Conn);//-----4
? ? ? ? ? ? object result = cmd.ExecuteScalar();//-----4
? ? ? ? ? ? Conn.Close();//-----4
? ? ? ? ? ? return result;//-----4
? ? ? ? }
? ? }
}
窗口1(主窗體)
Form2 form2=new Form2();
form2.form1=this;
窗口2?
public Form form1;
窗體3
Form3 form3=new Form3();
form3.mdiparent=form1;



5.20日記錄新的dbhelper
public static DataTable Select(string sql)
? ? ? ? {
? ? ? ? ? ? DataTable dt = new DataTable();
? ? ? ? ? ? SqlDataAdapter dap = new SqlDataAdapter(sql, strConn);
? ? ? ? ? ? dap.Fill(dt);
? ? ? ? ? ? return dt;
? ? ? ? }
pubulic static bool noselect (string sql){
sqlconnection conn=new sqlconnection(connstring);
sqlcommand cmd=new sqlcommand(sql,conn);
return cmd.executenonquery()>0
}


//窗體加載時(shí),綁定下拉列表 //從數(shù)據(jù)庫中獲取歌手的類型



檢驗(yàn)郵件@:
int hh=tbemail.text;
tbemail.text.indexof("@");
if? (hh<0)
if?(textBox6.Text.IndexOf("@")?!=?textBox6.Text.LastIndexOf("@")?||?textBox6.Text.IndexOf("@")==-1)
{
MessageBox.Show("郵箱格式輸入錯(cuò)誤!","錯(cuò)誤:",MessageBoxButtons.OK);
}

獲取每一行的數(shù)據(jù)?。?!
if(dt.rows.count>0) 再進(jìn)行上面步驟




foreach (DataRow item in dt.Rows)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this.cbb_type.Items.Add(item["SingerType"]);
? ? ? ? ? ? }





刪除的辦法:
DataGridViewRow dr = dataGridView1.CurrentRow;
? ? ? ? ? ? string id = dr.Cells[0].Value.ToString();
? ? ? ? ? ? string sql = string.Format(@"delete from student where name='{0}'", id);
? ? ? ? ? ? if (DBHelper.noselect(sql))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("刪除成功");
? ? ? ? ? ? }
? ? ? ? ? ? //DataGridViewRow dr = dataGridView1.CurrentRow;
? ? ? ? ? ? //string s = dr.Cells[0].Value.ToString();
? ? ? ? ? ? //string sql = string.Format("delete student where name = '{0}'", s);
? ? ? ? ? ? //if (DBHelper.noselect(sql))
? ? ? ? ? ? //{
? ? ? ? ? ? //? ? MessageBox.Show("刪除成功!");
? ? ? ? ? ? //? ? string sql2 = string.Format("select * from student");
? ? ? ? ? ? //? ? DataTable dt = DBHelper.select(sql2);
? ? ? ? ? ? //? ? dataGridView1.AutoGenerateColumns = false;
? ? ? ? ? ? //? ? dataGridView1.DataSource = dt;
? ? ? ? ? ? //}
? ? ? ? ? ? //else
? ? ? ? ? ? //? ? MessageBox.Show("刪除失??!");
? ? ? ? }
按確認(rèn)是否刪除
mbs 第一是中間? 第二是標(biāo)題? 第三是按鈕? 第四是圖片
