c#连接sqlserver数据库、插入数据、从数据库获取时间示例
作者:
c#连接sqlserver、插入数据、从数据库获取时间
using System;
using System.Data.SqlClient;
namespace Test
{
//连接数据库
public class Connection
{
private static string connectionString =
"Server = 192.168.1.222;" +
"Database = Test;" +
"User ID = Test;" +
"Password = abc123;";
/// <summary>
/// 连接数据库
/// </summary>
/// <returns></returns>
private SqlConnection ConnectionOpen()
{
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
return conn;
}
/// <summary>
/// 向表(Table)中插入一条数据
/// </summary>
public void Insert(string value1, string value2, string value3, DateTime dateTime)
{
SqlConnection conn = ConnectionOpen();
string sql =
"insert into Table(row1, row2, row3, DateTime) values ('" +
value1 + "', '" + value2 + "', '" + value3 + "', '" + dateTime + "')";
SqlCommand comm = new SqlCommand(sql, conn);
comm.ExecuteReader();
conn.Close();
}
/// <summary>
/// 从数据库中获取当前时间
/// </summary>
/// <returns></returns>
public DateTime GetDateTimeFromSQL()
{
SqlConnection conn = ConnectionOpen();
string sql = "select getdate()";
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader reader = comm.ExecuteReader();
DateTime dt;
if (reader.Read())
{
dt = (DateTime)reader[0];
conn.Close();
return dt;
}
conn.Close();
return DateTime.MinValue;
}
}
}
c#连接SQL Server 2008示例
/*
* 说明
* 功能说明:数据访问封装。所有数据都要通过这个类定义的dbConnection访问数据库。
* 同时,定义了通用的cmd,以及cmd常用的访问存储过程的方法RunPro
*
* 作者: RogerWang
*
* 创建日期:2006-02-15
*
*/
using System;
using System.Data;
using System.Data.SqlClient;
namespace insurer
{
/// <summary>
/// DataAccess 的摘要说明。
/// </summary>
public class DataAccess
{
private readonly string SQLCONNECTSTR = "server=(local);uid=sa;pwd=lwrong;database=insurer";
private SqlConnection dbConnection;
private readonly string RETUENVALUE = "RETURNVALUE";
//判断要不要启动事务
private bool startrans = false;
//为解决多笔数据导入的问题,特添加的事务处理属性
private SqlTransaction trans = null;
//定义是否启动事务属性
public bool StartTrans
{
get
{
return startrans;
}
set
{
startrans = value;
}
}
//定义事务
public SqlTransaction Trans
{
get
{
return trans;
}
set
{
if (value != null)
{
trans = value;
}
}
}
//创建打开dbConnection对象
public void OpenConnection()
{
if ( dbConnection == null )
{
dbConnection = new SqlConnection(SQLCONNECTSTR);
}
if ( dbConnection.State == ConnectionState.Closed )
{
try
{
dbConnection.Open();
}
catch(Exception ex)
{
SystemError.SystemLog(ex.Message);
}
finally
{
}
}
}
//释放dbConnection对象
public void CloseConnection()
{
if (dbConnection != null)
{
if (dbConnection.State == ConnectionState.Open)
{
dbConnection.Dispose();
dbConnection = null;
}
}
}
//
//创建cmd,注意dbconnection在该函数中创建,但没有在这函数中释放。
//在正确的面向对象设计方法中,对象应该是谁创建,谁就应该负责释放。按这个观点,这个过程有些不安全!!!!
private SqlCommand CreateCommand(string ProName,SqlParameter[] prams)
{
OpenConnection();
SqlCommand cmd = new SqlCommand(ProName,dbConnection);
cmd.CommandType = CommandType.StoredProcedure;
//如果进行事务处理,那么对cmd的Transaction的事务赋值
if (StartTrans)
{
cmd.Transaction = Trans;
}
if ( prams != null)
{
foreach(SqlParameter parameter in prams)
{
cmd.Parameters.Add(parameter);
}
}
//cmd.Parameters.Add(
return cmd;
}
/// <summary>
/// 创建cmd,并执行相应的操作。 然后释放cmd!
///
/// 该函数是执行cmd没有返回值,且没有参数的方法。
/// </summary>
/// <param name="ProName"></param>
public bool RunProc(string ProName)
{
SqlCommand cmd = CreateCommand(ProName,null);
bool k = false;
try
{
k = (bool)cmd.ExecuteScalar();
}
catch(Exception ex)
{
SystemError.SystemLog(ex.Message);
}
finally
{
cmd.Dispose();
}
return k;
}
/// <summary>
/// 创建cmd,并执行相应的操作。 然后释放cmd!
///
/// 该函数是执行cmd没有返回值,但有参数的方法。
/// </summary>
/// <param name="ProName"></param>
/// <param name="prams"></param>
public bool RunProc(string ProName,SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(ProName,prams);
bool k = false;
try
{
k = (bool) cmd.ExecuteScalar();
}
catch(Exception ex)
{
SystemError.SystemLog(ex.Message);
}
finally
{
cmd.Dispose();
//Close();
}
return k;
}
/// <summary>
/// 创建cmd,并执行相应的操作。 然后释放cmd!
///
/// 该函数是执行cmd带有返回值,但没有参数的方法。
/// </summary>
/// <param name="ProName"></param>
/// <param name="dataReader"></param>
public void RunProc(string ProName,out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(ProName,null);
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
try
{
}
catch(Exception ex)
{
SystemError.SystemLog(ex.Message);
}
finally
{
cmd.Dispose();
}
}
/// <summary>
/// 创建cmd,并执行相应的操作。 然后释放cmd!
///
/// 该函数是执行cmd带有返回值,且有参数的方法。
/// </summary>
/// <param name="ProName"></param>
/// <param name="prams"></param>
/// <param name="dataReader"></param>
public void RunProc(string ProName,SqlParameter[] prams,out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(ProName,prams);
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
try
{
}
catch(Exception ex)
{
SystemError.SystemLog(ex.Message);
}
finally
{
cmd.Dispose();
}
}
/// <summary>
/// 创建cmd的参数
/// 该方法的思路就是按条件生成一个SqlParameter对象。
/// 生成对象后,再给对象赋相应的返回值类型
/// </summary>
/// <param name="ParamName"></param>
/// <param name="DbType"></param>
/// <param name="size"></param>
/// <param name="direction"></param>
/// <param name="Value"></param>
/// <returns></returns>
public SqlParameter CreateParam(string ParamName, SqlDbType DbType, int size,ParameterDirection direction,object Value)
{
SqlParameter param;
if (size > 0)
{
param = new SqlParameter(ParamName,DbType,size);
}
else
{
param = new SqlParameter(ParamName,DbType);
}
param.Direction = direction;
param.Value = Value;
return param;
}
/// <summary>
/// 创建cmd的输入参数
/// </summary>
/// <param name="ParamName"></param>
/// <param name="DbType"></param>
/// <param name="size"></param>
/// <param name="Value"></param>
/// <returns></returns>
public SqlParameter CreateInParam(string ParamName, SqlDbType DbType, int size, object Value)
{
return CreateParam(ParamName,DbType,size,ParameterDirection.Input,Value);
}
/// <summary>
/// 创建cmd的输出参数
/// </summary>
/// <param name="ParamName"></param>
/// <param name="DbType"></param>
/// <param name="size"></param>
/// <returns></returns>
public SqlParameter CreateOutParam(string ParamName, SqlDbType DbType, int size)
{
return CreateParam(ParamName,DbType,size,ParameterDirection.Output,null);
}
/// <summary>
/// 创建cmd带有返回值的参数
/// </summary>
/// <param name="ParamName"></param>
/// <param name="DbType"></param>
/// <param name="size"></param>
/// <returns></returns>
public SqlParameter CreateReturnParam(string ParamName,SqlDbType DbType, int size)
{
return CreateParam(ParamName,DbType,size,ParameterDirection.ReturnValue,null);
}
//开始一个事务
public void BeginTrans()
{
OpenConnection();
Trans = dbConnection.BeginTransaction(IsolationLevel.Serializable);
}
public void Commit()
{
if (Trans != null)
{
Trans.Commit();
}
}
public void Rollback()
{
if (Trans != null)
{
Trans.Rollback();
}
}
}
}