C#的SQL操作類執行個體

來源:互聯網
上載者:User

標籤:基礎   gen   one   bsp   int   調用   nec   簡單方法   mat   

本文執行個體講述了C#的SQL操作類,分享給大家供大家參考。具體方法如下:


代碼如下:

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.SqlClient;

using System.Windows.Forms;

namespace yjgl

{

/// <summary>

/// 資料訪問基礎類(SQL)

/// </summary>

class BaseOperate

{

#region  建立資料庫連接

/// <summary>

/// 建立資料庫連接.

/// </summary>

/// <returns>返回SqlConnection對象</returns>

public SqlConnection getcon()

{

string M_str_sqlcon = “Data Source=PC-200201010009 ;Database=db_yjzl;User ID=sa;PWD=123”;//連接字串

SqlConnection mycon = new SqlConnection(M_str_sqlcon);      //建立連線物件

return mycon;

}

#endregion

#region  執行SqlCommand命令

/// <summary>

/// 執行SqlCommand

/// </summary>

/// <param name=”M_str_sqlstr”>SQL語句</param>

public void getcom(string M_str_sqlstr)

{

SqlConnection sqlcon = this.getcon();   //擷取連接字串

sqlcon.Open();       //開啟串連

try

{

SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon);  //通過SQL語句和連線物件建立命令對象

sqlcom.ExecuteNonQuery();     //對sqlcon執行SQL語句並返回受影響的行數

sqlcom.Dispose();           //釋放sqlcom’

}

catch (SqlException e)

{

MessageBox.Show(e.ToString(), “資訊提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);

}

finally

{

sqlcon.Close();

sqlcon.Dispose();

}

}

#endregion

#region  建立DataSet對象

/// <summary>

/// 建立一個DataSet對象

/// </summary>

/// <param name=”M_str_sqlstr”>SQL語句</param>

/// <param name=”M_str_table”>表名</param>

/// <returns>返回DataSet對象</returns>

public DataSet getds(string M_str_sqlstr, string M_str_table)  //查詢

{

SqlConnection sqlcon = this.getcon();

SqlDataAdapter sqlda = new SqlDataAdapter(M_str_sqlstr, sqlcon);

DataSet myds = new DataSet();

sqlda.Fill(myds, M_str_table);

return myds;

}

#endregion

#region  建立SqlDataReader對象

/// <summary>

/// 建立一個SqlDataReader對象

/// </summary>

/// <param name=”M_str_sqlstr”>SQL語句</param>

/// <returns>返回SqlDataReader對象</returns>

public SqlDataReader getread(string M_str_sqlstr)

{

SqlConnection sqlcon = this.getcon();

SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon);

sqlcon.Open();

SqlDataReader sqlread = sqlcom.ExecuteReader(CommandBehavior.CloseConnection);

return sqlread;

}

#endregion

}

}

希望本文所述對大家的C#程式設計有所協助。

除聲明外, 跑步客文章均為原創,轉載請以連結形式標明本文地址
  C#的SQL操作類執行個體

本文地址:  http://www.paobuke.com/develop/c-develop/pbk23442.html






相關內容c# 動態載入dll檔案,並實現調用其中的簡單方法C#中用foreach語句遍曆數組及將數組作為參數的用法C#編程實現查看剪下板內容的方法DirectoryEntry????IIS73???ADSI Error£o?′?a′í?ó(0x80005000)
C#將DataTable轉換成list的方法Winform中GridView分組排序功能實現方法C#中const用法詳解C#中datatable序列化與還原序列化執行個體分析

C#的SQL操作類執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.