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") ......
由于项目需要,用到其他项目组用VC开发的组件,在web后台代码无法访问这个组件,所以只好通过后台调用前台的javascript,从而操作这个组件。在网上找了找,发现有三种方法可以访问到前台代码:
第一种,OnClientClick (vs2003不支持这个方法)
<asp:Button ID="Button1" runat="se ......
方法一:遍历正在有相同名字运行的例程
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace DHPSS.COM
{
/// <summary>
/// c#程序只运行一次,C#例程只运行一次
/// http://blog.csdn.net/nnsw ......
先声明,我不是这方面的专家,只是干软件四个月不知天有多高地有多硬的小孩子
首先,类型转换是有一定开销的
我试过这样的代码:这是一个控制台应用程序的Program类的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 类型转换的开销
{
class Program
......
动态加载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 ......