自學(xué)筆記

connection對(duì)象

第一步導(dǎo)入兩個(gè)命名空間:

using.system.data

using.system.data.sqlclient

command對(duì)象


第二章內(nèi)容


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();

? ? ? ? }


第三章作業(yè)



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;

顯示dataview數(shù)據(jù)


模糊查詢


模糊查詢的另一種方式

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ù)庫中獲取歌手的類型


load事件:光標(biāo)直接在查詢的地方


檢驗(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)行上面步驟




顯示下拉框里面的東西,從數(shù)據(jù)庫中提取!


foreach (DataRow item in dt.Rows)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? this.cbb_type.Items.Add(item["SingerType"]);

? ? ? ? ? ? }


displaymember是控件里面顯示出來的,valuemember是數(shù)據(jù)庫里面的





DataGridView很詳細(xì)的用法

刪除的辦法:

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)題? 第三是按鈕? 第四是圖片


datatable與dataset的區(qū)別

DataSet & DataTable &DataRow 深入淺出

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 導(dǎo)入兩個(gè)命名空間 using system.data; using system.data.sqlclient; ...
    程序媛_閱讀 771評(píng)論 0 0
  • 該類為本人項(xiàng)目使用中的oracle連接工具類 1.先引入OraOps10.dll 2.將該類加入項(xiàng)目中 dll及源...
    羚羊藏歷閱讀 1,373評(píng)論 0 0
  • #DBHelp ``` public class DBHelper { //數(shù)據(jù)庫連接字符串 private...
    暗組夏天閱讀 2,669評(píng)論 0 1
  • using System; using System.Collections.Generic; using Sys...
    程序媛_閱讀 306評(píng)論 0 0
  • using System; using System.Collections.Generic; using Sys...
    程序媛_閱讀 254評(píng)論 0 0

友情鏈接更多精彩內(nèi)容