欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#操作數(shù)據(jù)庫總結(jié)(vs2005+sql2005)

 更新時(shí)間:2012年09月24日 20:10:05   作者:  
C#操作數(shù)據(jù)庫總結(jié),每次做項(xiàng)目都會(huì)用到數(shù)據(jù)庫,對數(shù)據(jù)庫的操作都是糊里糊涂從書里找代碼用。通過昨天晚上與今天早上的努力,把數(shù)據(jù)庫的操作整理了一下,下面把整理結(jié)果做個(gè)小結(jié)

開發(fā)工具:Microsoft Visual Studio 2005
數(shù)據(jù)庫:Microsoft SQL Server 2005
說明:這里建立的數(shù)據(jù)庫名為Demo,有一個(gè)學(xué)生表Student,為操作方便起見,我只添加兩個(gè)字段:studentnum和studentname.
一、SQL語句:

復(fù)制代碼 代碼如下:

--create database Demo
use Demo

create table Student
(
studentnum char(14) primary key,
studentname varchar(30) not null
)
insert into Student values('20041000010201','張揚(yáng)')

二、代碼:
1.引入名稱空間:using System.Data.SqlClient;
2.定義連接字符串,連接對象,命令對象:
private String connectionstr;
private SqlConnection connection;
private SqlCommand command;
3.在構(gòu)造函數(shù)中初始化連接字符串,連接對象,命令對象

(1)初始化連接字符串:
方式① connectionstr="server=localhost;uid=sa;pwd=123456;database=Demo";
方式② connectionstr="server=127.0.0.1";Integrade Security=SSPI;database=Demo";
其中,SIMS是我要連接的數(shù)據(jù)庫名.(1)中的uid 和pwd是你登錄數(shù)據(jù)庫的登錄名和密碼
注:這種連接是連接本地的數(shù)據(jù)庫,若要連接局域網(wǎng)內(nèi)其它機(jī)子上的數(shù)據(jù)庫,可將方式①的"server=localhost;"改為"server=數(shù)據(jù)庫所在機(jī)子的IP;"
復(fù)制代碼 代碼如下:

// 連接字符串:String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=product.mdb";
// 建立連接:OleDbConnection connection = new OleDbConnection(connectionString);
// 使用OleDbCommand類來執(zhí)行Sql語句:
// OleDbCommand cmd = new OleDbCommand(sql, connection);
// connection.Open();
// cmd.ExecuteNonQuery();
#endregion

#region 連接字符串
//string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\程序書籍軟件\c#程序代碼\access數(shù)據(jù)庫操作\addressList.mdb"; //絕對路徑
// string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Environment.CurrentDirectory+"\\addressList.mdb"; //相對路徑


(2)初始化連接對象
connection = new SqlConnection(connectionstr);
(3)初始化命令對象
command =new SqlCommand();
command .Connection =connection ;
4.操作數(shù)據(jù)庫中的數(shù)據(jù)
(1)查詢數(shù)據(jù)庫中的數(shù)據(jù)
方法一:
復(fù)制代碼 代碼如下:

string snum=tBstudentnum .Text .Trim ();
string str = "select * from Student where studentnum='" + snum + "'";
command .CommandText =str;
connection.Open();
if (command.ExecuteScalar() == null)
{
MessageBox.Show("您輸入的學(xué)號對應(yīng)的學(xué)生不存在!", "錯(cuò)誤", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
SqlDataReader sdr = command.ExecuteReader();
while (sdr.Read())
{
tBstudentnum .Text = sdr["studentnum"].ToString();
tBstudentname.Text = sdr["studentname"].ToString();
}
sdr.Close();
}
connection.Close();

方法二:
復(fù)制代碼 代碼如下:

string snum=tBstudentnum .Text .Trim ();
string str = "select * from Student where studentnum='" + snum + "'";
command .CommandText =str;
connection.Open();
if (command.ExecuteScalar() == null)
{
MessageBox.Show("您輸入的學(xué)號對應(yīng)的學(xué)生不存在!", "錯(cuò)誤", MessageBoxButtons.OK,MessageBoxIcon.Error);

}
else
{
SqlDataAdapter sda = new SqlDataAdapter(str,connection );
DataSet ds = new DataSet();
sda.Fill(ds, "Student");
DataTable dt = ds.Tables["Student"];
tBstudentnum.Text = dt.Rows[0]["studentnum"].ToString();
tBstudentname.Text = dt.Rows[0]["studentname"].ToString();
}
connection.Close();

(2)向數(shù)據(jù)庫中添加數(shù)據(jù)
方法一:
復(fù)制代碼 代碼如下:

string snum = tBstudentnum.Text.Trim ();
string sname = tBstudentname.Text.Trim();
if (snum == "" || sname == "")
{
MessageBox.Show("學(xué)生學(xué)號或姓名不能為空!", "錯(cuò)誤", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
string insertstr="insert into Student values('"+snum +"','"+sname +"')";
command.CommandText = insertstr;
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("學(xué)生添加成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
connection.Close();
}

方法二:
復(fù)制代碼 代碼如下:

string str = "select * from Student";
string insertstr = "insert into Student values('" + snum + "','" + sname + "')";
SqlDataAdapter sda = new SqlDataAdapter(str, connection);
DataSet ds = new DataSet();
sda.Fill(ds, "Student");
DataTable dt = ds.Tables["Student"];
DataRow dr = dt.NewRow();
dr["studentnum"] = snum;
dr["studentname"] = sname;
dt.Rows.Add(dr);
sda.InsertCommand = new SqlCommand(insertstr, connection);
sda.Update(ds, "Student");
MessageBox.Show("學(xué)生添加成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);

(3)修改數(shù)據(jù)庫中的數(shù)據(jù)
方法一:
復(fù)制代碼 代碼如下:

string snum = tBstudentnum.Text.Trim();
string sname = tBstudentname.Text.Trim();
if (snum == "" || sname == "")
{
MessageBox.Show("學(xué)生學(xué)號或姓名不能為空!", "錯(cuò)誤", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
string modifystr = "update Student set studentname='" + sname +
"' where studentnum='" + snum + "'";
command.CommandText = modifystr;
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("學(xué)生的姓名修改成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information );
connection.Close();

方法二:
復(fù)制代碼 代碼如下:

string snum = tBstudentnum.Text.Trim();
string sname = tBstudentname.Text.Trim();
if (snum == "" || sname == "")
{
MessageBox.Show("學(xué)生學(xué)號或姓名不能為空!", "錯(cuò)誤", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
string str = "select * from Student where studentnum='" + snum + "'"; ;
string updatestr = "update Student set studentname='" + sname +
"' where studentnum='" + snum + "'";
SqlDataAdapter sda = new SqlDataAdapter(str, connection);
DataSet ds = new DataSet();
sda.Fill(ds, "Student");
DataTable dt = ds.Tables["Student"];
dt.Rows[0]["studentname"] = sname;
sda.UpdateCommand = new SqlCommand(updatestr , connection);
sda.Update(ds, "Student");
MessageBox.Show("學(xué)生姓名修改成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}

(4)刪除數(shù)據(jù)庫中的數(shù)據(jù)
方法一:
復(fù)制代碼 代碼如下:

string snum = tBstudentnum.Text.Trim();
if (snum == "")
{
MessageBox.Show("學(xué)生學(xué)號不能為空!", "錯(cuò)誤", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
string str = "select * from Student where studentnum='" + snum + "'";
string deletestr = "delete from Student where studentnum='" + snum + "'";
command.CommandText =str ;
connection.Open();
if (command.ExecuteScalar() == null)
{
MessageBox.Show("此學(xué)號對應(yīng)的學(xué)生不存在!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
command.CommandText = deletestr;
command.ExecuteNonQuery();
MessageBox.Show("學(xué)生的信息刪除成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
connection.Close();

方二:
復(fù)制代碼 代碼如下:

string str = "select * from Student where studentnum='" + snum + "'";
string deletestr = "delete from Student where studentnum='" + snum + "'";
SqlDataAdapter sda = new SqlDataAdapter(str, connection);
DataSet ds = new DataSet();
sda.Fill(ds, "Student");
DataTable dt = ds.Tables["Student"];
if (dt.Rows.Count > 0)
{
dt.Rows[0].Delete();
sda.DeleteCommand = new SqlCommand(deletestr, connection);
sda.Update(ds, "Student");
MessageBox.Show("學(xué)生信息刪除成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
MessageBox.Show("此學(xué)號對應(yīng)的學(xué)生不存在!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

相關(guān)文章

  • C# OpenFileDialog對話框控件的使用

    C# OpenFileDialog對話框控件的使用

    OpenFileDialog是C#中常用的對話框控件,用于讓用戶選擇文件,本文就來介紹一下OpenFileDialog對話框控件的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • C#裝箱和拆箱的原理介紹

    C#裝箱和拆箱的原理介紹

    這篇文章介紹了C#裝箱和拆箱的原理,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • C#實(shí)現(xiàn)MQTT服務(wù)端與客戶端通訊功能

    C#實(shí)現(xiàn)MQTT服務(wù)端與客戶端通訊功能

    這篇文章介紹了C#實(shí)現(xiàn)MQTT服務(wù)端與客戶端通訊的功能,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • C#實(shí)現(xiàn)為一張大尺寸圖片創(chuàng)建縮略圖的方法

    C#實(shí)現(xiàn)為一張大尺寸圖片創(chuàng)建縮略圖的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)為一張大尺寸圖片創(chuàng)建縮略圖的方法,涉及C#創(chuàng)建縮略圖的相關(guān)圖片操作技巧,需要的朋友可以參考下
    2015-06-06
  • C#?WPF數(shù)據(jù)綁定模板化操作的完整步驟

    C#?WPF數(shù)據(jù)綁定模板化操作的完整步驟

    WPF中的數(shù)據(jù)綁定提供了很強(qiáng)大的功能,與普通的WinForm程序相比,其綁定功能為我們提供了很多便利,下面這篇文章主要給大家介紹了關(guān)于C#?WPF數(shù)據(jù)綁定模板化操作的完整步驟,需要的朋友可以參考下
    2022-01-01
  • c#配置文件中自定義塊節(jié)點(diǎn)的方法

    c#配置文件中自定義塊節(jié)點(diǎn)的方法

    在.netcore中我們非常方便獲取配置文件節(jié)點(diǎn)參數(shù),但是在非.netcore下一般 我們是使用中方法,本文我們將給大家詳細(xì)介紹c#配置文件中自定義塊節(jié)點(diǎn)的方法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-10-10
  • C#實(shí)現(xiàn)冒泡排序算法的代碼示例

    C#實(shí)現(xiàn)冒泡排序算法的代碼示例

    冒泡排序即是對數(shù)組每次輪循出最大數(shù)或最小數(shù)放在隊(duì)尾,這里我們來看一下C#實(shí)現(xiàn)冒泡排序算法的代碼示例,需要的朋友可以參考下
    2016-07-07
  • C#和Java中二維數(shù)組區(qū)別分析

    C#和Java中二維數(shù)組區(qū)別分析

    這篇文章主要介紹了C#和Java中二維數(shù)組區(qū)別分析,主要講述了二維數(shù)組在C#和Java中定義及應(yīng)用的區(qū)別,非常實(shí)用,需要的朋友可以參考下
    2014-10-10
  • C#使用DateTime獲取日期和時(shí)間的實(shí)現(xiàn)

    C#使用DateTime獲取日期和時(shí)間的實(shí)現(xiàn)

    在C#中,DateTime類是用來處理日期和時(shí)間的類,它具有許多屬性和方法,用于操作和獲取日期和時(shí)間的不同部分,本文就來介紹一下C#使用DateTime獲取,感興趣的可以了解一下
    2023-11-11
  • C# TcpClient網(wǎng)絡(luò)編程傳輸文件的示例

    C# TcpClient網(wǎng)絡(luò)編程傳輸文件的示例

    這篇文章主要介紹了C# TcpClient網(wǎng)絡(luò)編程傳輸文件的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04

最新評論