易截截图软件、单文件、免安装、纯绿色、仅160KB

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();
}
}


相关文档:

C#中结构与类的区别

一.类与结构的示例比较:
结构示例:
public struct Person
{
string Name;
int height;
int weight
public bool overWeight()
{
//implement something
}
}
类示例:
public class TestTime
{
int hours;
int minutes;
int seconds;
public void passtime()
{
//implementation ......

130道c#面试题


1. 简述 private、 protected、 public、 internal 修饰符的访问权限。
答 . private : 私有成员, 在类的内部才可以访问。 
protected : 保护成员,该类内部和继承类中可以访问。 
public : 公共成员,完全公开,没有访问限制。 
internal: 在同一命名空间内可以访问。
 
 
2 .列举ASP.N ......

Observer 观察者模式 (HeadFirst设计模式 c#)

Observer.cs
using System;
using System.Text;
using System.Collections.Generic;
namespace Observer
{
   
    public interface ISubject
    {
        void RegisterObserver(IOvserver o);
    & ......

c#事务回滚

c#事务回滚(转)
作者:xue5ya  来源:博客园  发布时间:2009-03-20 16:08  阅读:263 次  原文链接   [收藏]  
Code
public void UpdateContactTableByDataSet(DataSet ds,string strTblName) 

    ......

C#——调用存储过程方法

下面是我自己总结的一个比较简单的c#调用SQL Server 2005存储过程的小例子,主要是介绍整个过程,有问题的话请大虾们指导,谢谢~
作者:shinehoo
1)存储过程这样写的
ALTER PROCEDURE dbo.procShowLog
    (
    @StartTime datetime,
    @EndTime datetime,
  ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号