C#——访问SQL Server 2005公共类
下面是我总结出来的一个数据库访问公共类,基于ADO.NET,C#的,其中,以重载的方式实现使用存属过程的接口和不用存储过程的接口,如有不妥请大家指正,谢谢~
作者:shinehoo
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace shinehoo.db
{
/// <summary>
/// 对数据库的一些公共操作
/// </summary>
public class DBOperate
{
/// <summary>
/// 建立数据库连接
/// </summary>
/// <returns>返回SqlConnection对象</returns>
public static SqlConnection getConnection()
{
SqlConnection myCon;
try
{
string strSqlCon = "Data Source = SHINEHOO-PC\\SQLEXPRESS; Initial Catalog = ShineHoo_DB; Integrated Security = True";
//SqlConnection类用来连接数据库
myCon = new SqlConnection(strSqlCon);
}
catch (Exception e)
{
throw e;
}
return myCon;
}
/// <summary>
/// 执行SqlCommand命令
/// </summary>
/// <param name="strSqlCommand">SQL语句</param>
public static void getCommand(string strSqlCommand)
{
SqlConnection sqlcon = getConnection();
try
{
//SqlConnection类的Open()方法用来打开数据库连接
sqlcon.Open();
//声明将对数据库执行一个SQL语句或存储过程
SqlCommand sqlcom = new SqlCommand(strSqlCommand, sqlcon);
//执行SqlCommand命令
sqlcom.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
//关闭数据库连接
sqlcon.Close();
//sqlcon.Dispose();
}
}
相关文档:
一、Access从Excel中导入数据
1.用到的Excel表的格式及内容
实现
OleDbConnection con = new OleDbConnection();
try
{
OpenFileDialog openFile = new OpenFileDialog();//打开文件对话框。
openFile.Filter = ("Excel 文件(*.xls)|*.xls") ......
关于使用到了两个C#关键字this和base。
1,C# "this " keyword
其作用引用类的当前实例,其实看了下面这个例子就好理解了。
主要三个作用:当前实例、参数传递和索引器
1.1 当前实例
class Team
{
///成员变量
private string name;
///构造函数
......
public string WriteXML(string[] values, int flag)
{
//如果flag==0则为第一次运行需要初始化XML文件
if (flag == 0)
{
//生在随机文件名
string dateName = System.DateTime.Now.ToString("yyyyMMddHHmmss");
......
你首先要理解一下概念:
一 类型(Type) 对象是什么
比如 object x; x是对象,object就是它的类型,在程序中如何描述类型这个概念呢?
就是Type(System.Type)。要获取某个类的类型可以用typeof()操作符
object a;object b; DataTable t;
aType = typeof(object);Type bType = typeof(object);tType = typ ......
动态加载DLL需要使用Windows API函数:LoadLibrary、GetProcAddress以及FreeLibrary。我们可以使用DllImport在C#中使用这三个函数。
[DllImport("Kernel32")]
public static extern int GetProcAddress(int handle, String funcname);
[DllImport("Kernel32")]
public static extern int L ......