C# SQL数据库操作通用类
C# SQL数据库操作通用类
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace Framework.DataBase
{
///
/// 通用数据库类
///
public class DataBase
{
private string ConnStr = null;
public DataBase()
{
ConnStr = ConfigurationSettings.AppSettings["ConnStr"];
}
public DataBase(string Str)
{
try
{
this.ConnStr = Str;
}
catch(Exception ex)
{
throw ex;
}
}
///
/// 返回connection对象
///
///
public SqlConnection ReturnConn()
{
SqlConnection Conn = new SqlConnection(ConnStr);
Conn.Open();
return Conn;
}
public void Dispose(SqlConnection Conn)
{
if(Conn!=null)
{
Conn.Close();
Conn.Dispose();
}
}
///
/// 运行SQL语句
///
///
public void RunProc(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd ;
Cmd = CreateCmd(SQL, Conn);
try
{
Cmd.ExecuteNonQuery();
}
catch
{
throw new Exception(SQL);
}
Dispose(Conn);
return;
}
///
/// 运行SQL语句返回DataReader
///
///
/// SqlDataReader对象.
public SqlDataReader RunProcGetReader(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd ;
Cmd = CreateCmd(SQL, Conn);
SqlDataReader Dr;
try
{
Dr = Cmd.ExecuteReader(CommandBehavior.Default);
}
catch
相关文档:
文章原创,转载请与Blog主人联系,robin9257@hotmail.com
JAVA与SQL桥接的优缺点:
1、优点:不用下载驱动程序,允许用相同代码访问不同DBMS。
2、缺点:效率低。
经常出现的报错点:
1、java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
&nbs ......
bit:0或1的整型数字
int:从-2^31(-2,147,483,648)到2^31(2,147,483,647)的整型数字
smallint:从-2^15(-32,768)到2^15(32,767)的整型数字
tinyint:从0到255的整型数字
decimal:从-10^38到10^38-1的定精度与有效位数的数字
numeric:decimal的同义词
money:从-2^63(-922,337,203,685,477.580 ......
在我们的日常编程中,数据库的程序基本上都要与SQL语句打交道,SQL语句的编写不可避免的成为一个头疼的工作。且因为SQL语句是STRING类型,因此在编译阶段查不出错,只有到运行时才能发现错误。
本文的解决方案,通过自动生成SQL语句,在一定程度上降低出错的概率,从而提高编程效率。 public int ......
将b表中caller列的值插入a表中call列中。
create table a
(
fid int,
call varchar(20),
age int
)
create table b
(
fid int,
caller varchar(20),
parentId int
)
select * from a
select * from b
insert into a values(1,null,19)
insert into a values(2,n ......