C#數(shù)據(jù)庫(kù)連接方式(類的形式)
更新時(shí)間:2023年07月12日 10:25:34 作者:不想學(xué)習(xí)只想玩
這篇文章主要介紹了C#數(shù)據(jù)庫(kù)連接方式(類的形式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
C#數(shù)據(jù)庫(kù)連接(類的形式)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace systemprotect
{
class DataCon
{
static string strCon = "server=.;database =自己的數(shù)據(jù)庫(kù)名;uid=sa;pwd=自己的數(shù)據(jù)庫(kù)密碼";//數(shù)據(jù)庫(kù)連接串
SqlConnection conn = new SqlConnection(strCon);
public SqlDataReader query(string str)//查詢
{
if (this.conn.State == System.Data.ConnectionState.Closed)//判斷連接是否打開
{
this.conn.Open();
}
SqlCommand cmd = new SqlCommand(str, conn);
return cmd.ExecuteReader();
}
public int insert(string str)//插入,刪除,更新 返回影響的行數(shù)
{
if (this.conn.State == System.Data.ConnectionState.Closed)
{
this.conn.Open();
}
SqlCommand cmd = new SqlCommand(str, conn);
return cmd.ExecuteNonQuery();
}
public void close()//關(guān)閉連接
{
conn.Close();
}
}
}C#連接數(shù)據(jù)庫(kù)的步驟和相關(guān)的方法調(diào)用
//第一步:創(chuàng)建Connection 數(shù)據(jù)庫(kù)連接對(duì)象
SqlConnection conn = new SqlConnection("server = . ; uid = sa ; pwd = jnos;database = JINGDONGDB");
//第二步:打開連接數(shù)據(jù)庫(kù)
conn.Open();
//第三步:使用數(shù)據(jù)庫(kù)
string sql = $@"select ProductNo, ProductName, ProductImage,
Price,password from Product where ProductNo={_ProductNo}and password='{_password}'";//@符號(hào)表示可以換行,代碼也連接在一起
SqlCommand command = new SqlCommand(sql,conn);
SqlDataReader reader = command.ExecuteReader();//
if (reader.Read())
{
string ProductNo = reader["ProductNo"].ToString();
string ProductName = reader["ProductName"].ToString();
MessageBox.Show($"歡迎{ProductName}登錄成功");
}//在數(shù)據(jù)庫(kù)里面是為 next() 有數(shù)據(jù)為 true 沒有數(shù)據(jù)為 flase
else
{
MessageBox.Show("賬號(hào)或密碼錯(cuò)誤,請(qǐng)重新輸入!");
}
//int resule = command.ExecuteNonQuery();//添加、刪除、修改(返回行數(shù)受影響)接SqlCommand command = new SqlCommand(sql,conn);
//行 是添加刪除修改的步驟
//object result = command.ExecuteScalar();//查詢聚合函數(shù)用到
//if(resule > 0)
//{
// MessageBox.Show("刪除成功");
//}
//else
//{
// MessageBox.Show("刪除失敗");
//}
//第四步:關(guān)閉連接對(duì)象
conn.Close();相關(guān)的方法調(diào)用
command.ExecuteNonQuery:添加、刪除、修改(返回行數(shù)受影響command.ExecuteScalar:查詢聚合函數(shù)command.ExecuteReader:查詢單列函數(shù)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)將PDF轉(zhuǎn)為線性化PDF
線性化PDF文件是PDF文件的一種特殊格式,可以通過(guò)Internet更快地進(jìn)行查看。這篇文章主要介紹了如何通過(guò)C#實(shí)現(xiàn)將PDF轉(zhuǎn)為線性化PDF,感興趣的小伙伴可以學(xué)習(xí)一下2021-12-12
C#代碼實(shí)現(xiàn)Word文檔文本批量替換(動(dòng)態(tài)填充)
在各類企業(yè)級(jí)應(yīng)用中,程序化修改?Word?文檔是一個(gè)高頻需求,Free?Spire.Doc?for?.NET?是一套在無(wú)需本地安裝?Microsoft?Office?環(huán)境下直接操作?Word?文檔的免費(fèi)?.NET?API,下面我們就來(lái)看看如何使用它實(shí)現(xiàn)批量替換Word文本吧2026-05-05
基于C#實(shí)現(xiàn)PDF按頁(yè)分割文件和分頁(yè)合并
iTextSharp 是一個(gè)開源的 PDF 處理庫(kù),用于在 C# 程序中創(chuàng)建、編輯和處理 PDF 文件,本文將使用iTextSharp實(shí)現(xiàn)C# PDF分割與合并,感興趣的可以了解下2025-03-03
基于Unity3D實(shí)現(xiàn)3D迷宮小游戲的示例代碼
迷宮游戲作為經(jīng)典的小游戲,一直深受大家的喜愛。本文小編將為大家詳細(xì)介紹一下如何用Unity實(shí)現(xiàn)一個(gè)3D版的迷宮小游戲,感興趣的可以動(dòng)手試一試2022-03-03
C#實(shí)現(xiàn)HTTP訪問類HttpHelper的示例詳解
在項(xiàng)目開發(fā)過(guò)程中,我們經(jīng)常會(huì)訪問第三方接口,如我們需要接入的第三方接口是Web API,這時(shí)候我們就需要使用HttpHelper調(diào)用遠(yuǎn)程接口了。本文為大家介紹了C#實(shí)現(xiàn)HTTP訪問類HttpHelper的示例代碼,需要的可以參考一下2022-09-09

