用c#实现增,删,改,差并连接数据库
答案:3 悬赏:30 手机版
解决时间 2021-03-15 03:01
- 提问者网友:轻浮
- 2021-03-14 06:46
用c#实现增,删,改,差并连接数据库
最佳答案
- 五星知识达人网友:慢性怪人
- 2021-03-14 08:02
指明要用五函数
Connecting
Command
Data Adapter
Data Set
Data Table
前期连接SQL数据库比Sqlconnection
楼谢谢清楚
问题插入元组
要写
'" + aa + "'
能理解
能直接'aa'加号意思
望指教
Connecting
Command
Data Adapter
Data Set
Data Table
前期连接SQL数据库比Sqlconnection
楼谢谢清楚
问题插入元组
要写
'" + aa + "'
能理解
能直接'aa'加号意思
望指教
全部回答
- 1楼网友:酒者煙囻
- 2021-03-14 09:42
using system;
using system.collections;
using system.collections.specialized;
using system.data;
using system.data.sqlclient;
using system.configuration;
using system.data.common;
using system.collections.generic;
namespace ceptauto.dbutility
{
///
/// 数据访问抽象基础类
/// copyright (c) 2004-2008 by litianping
///
public abstract class dbhelpersql
{
//数据库连接字符串(web.config来配置),可以动态更改connectionstring支持多数据库.
public static string connectionstring = pubconstant.connectionstring;
public dbhelpersql()
{
}
#region 公用方法
///
/// 判断是否存在某表的某个字段
///
/// 表名称
/// 列名称
/// 是否存在
public static bool columnexists(string tablename, string columnname)
{
string sql = "select count(1) from syscolumns where [id]=object_id('" + tablename + "') and [name]='" + columnname + "'";
object res = getsingle(sql);
if (res == null)
{
return false;
}
return convert.toint32(res) > 0;
}
public static int getmaxid(string fieldname, string tablename)
{
string strsql = "select max(" + fieldname + ")+1 from " + tablename;
object obj = getsingle(strsql);
if (obj == null)
{
return 1;
}
else
{
return int.parse(obj.tostring());
}
}
public static bool exists(string strsql)
{
object obj = getsingle(strsql);
int cmdresult;
if ((object.equals(obj, null)) || (object.equals(obj, system.dbnull.value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.parse(obj.tostring());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
///
/// 表是否存在
///
///
///
public static bool tabexists(string tablename)
{
string strsql = "select count(*) from sysobjects where id = object_id(n'[" + tablename + "]') and objectproperty(id, n'isusertable') = 1";
//string strsql = "select count(*) from sys.objects where object_id = object_id(n'[dbo].[" + tablename + "]') and type in (n'u')";
object obj = getsingle(strsql);
int cmdresult;
if ((object.equals(obj, null)) || (object.equals(obj, system.dbnull.value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.parse(obj.tostring());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
public static bool exists(string strsql, params sqlparameter[] cmdparms)
{
object obj = getsingle(strsql, cmdparms);
int cmdresult;
if ((object.equals(obj, null)) || (object.equals(obj, system.dbnull.value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.parse(obj.tostring());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
#endregion
#region 执行简单sql语句
///
/// 执行sql语句,返回影响的记录数
///
/// sql语句
/// 影响的记录数
public static int executesql(string sqlstring)
{
using (sqlconnection connection = new sqlconnection(connectionstring))
{
using (sqlcommand cmd = new sqlcommand(sqlstring, connection))
{
try
{
connection.open();
int rows = cmd.executenonquery();
return rows;
}
catch (system.data.sqlclient.sqlexception e)
{
connection.close();
throw e;
}
}
}
}
public static int executesqlbytime(string sqlstring, int times)
{
using (sqlconnection connection = new sqlconnection(connectionstring))
{
using (sqlcommand cmd = new sqlcommand(sqlstring, connection))
{
try
{
connection.open();
cmd.commandtimeout = times;
int rows = cmd.executenonquery();
return rows;
}
catch (system.data.sqlclient.sqlexception e)
{
connection.close();
throw e;
}
}
}
}
///
/// 执行多条sql语句,实现数据库事务。
///
/// 多条sql语句
public static int executesqltran(list sqlstringlist)
{
using (sqlconnection conn = new sqlconnection(connectionstring))
{
conn.open();
sqlcommand cmd = new sqlcommand();
cmd.connection = conn;
sqltransaction tx = conn.begintransaction();
cmd.transaction = tx;
try
{
int count = 0;
for (int n = 0; n < sqlstringlist.count; n++)
{
string strsql = sqlstringlist[n];
if (strsql.trim().length > 1)
{
cmd.commandtext = strsql;
count += cmd.executenonquery();
}
}
tx.commit();
return count;
}
catch
{
tx.rollback();
return 0;
}
}
}
///
/// 执行带一个存储过程参数的的sql语句。
///
/// sql语句
/// 参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加
/// 影响的记录数
public static int executesql(string sqlstring, string content)
{
using (sqlconnection connection = new sqlconnection(connectionstring))
{
sqlcommand cmd = new sqlcommand(sqlstring, connection);
system.data.sqlclient.sqlparameter myparameter = new system.data.sqlclient.sqlparameter("@content", sqldbtype.ntext);
myparameter.value = content;
cmd.parameters.add(myparameter);
try
{
connection.open();
int rows = cmd.executenonquery();
return rows;
}
catch (system.data.sqlclient.sqlexception e)
{
throw e;
}
finally
{
cmd.dispose();
connection.close();
}
}
}
///
/// 执行带一个存储过程参数的的sql语句。
///
/// sql语句
/// 参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加
/// 影响的记录数
public static object executesqlget(string sqlstring, string content)
{
using (sqlconnection connection = new sqlconnection(connectionstring))
{
sqlcommand cmd = new sqlcommand(sqlstring, connection);
system.data.sqlclient.sqlparameter myparameter = new system.data.sqlclient.sqlparameter("@content", sqldbtype.ntext);
myparameter.value = content;
cmd.parameters.add(myparameter);
try
{
connection.open();
object obj = cmd.executescalar();
if ((object.equals(obj, null)) || (object.equals(obj, system.dbnull.value)))
{
return null;
}
else
{
return obj;
}
}
catch (system.data.sqlclient.sqlexception e)
{
throw e;
}
finally
{
cmd.dispose();
connection.close();
}
}
}
///
/// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
///
/// sql语句
/// 图像字节,数据库的字段类型为image的情况
/// 影响的记录数
public static int executesqlinsertimg(string strsql, byte[] fs)
{
using (sqlconnection connection = new sqlconnection(connectionstring))
{
sqlcommand cmd = new sqlcommand(strsql, connection);
system.data.sqlclient.sqlparameter myparameter = new system.data.sqlclient.sqlparameter("@fs", sqldbtype.image);
myparameter.value = fs;
cmd.parameters.add(myparameter);
try
{
connection.open();
int rows = cmd.executenonquery();
return rows;
}
catch (system.data.sqlclient.sqlexception e)
{
throw e;
}
finally
{
cmd.dispose();
connection.close();
}
}
}
///
/// 执行一条计算查询结果语句,返回查询结果(object)。
///
/// 计算查询结果语句
/// 查询结果(object)
public static object getsingle(string sqlstring)
{
using (sqlconnection connection = new sqlconnection(connectionstring))
{
using (sqlcommand cmd = new sqlcommand(sqlstring, connection))
{
try
{
connection.open();
object obj = cmd.executescalar();
if ((object.equals(obj, null)) || (object.equals(obj, system.dbnull.value)))
{
return null;
}
else
{
return obj;
}
}
catch (system.data.sqlclient.sqlexception e)
{
connection.close();
throw e;
}
}
}
}
public static object getsingle(string sqlstring, int times)
{
using (sqlconnection connection = new sqlconnection(connectionstring))
{
using (sqlcommand cmd = new sqlcommand(sqlstring, connection))
{
try
{
connection.open();
cmd.commandtimeout = times;
object obj = cmd.executescalar();
if ((object.equals(obj, null)) || (object.equals(obj, system.dbnull.value)))
{
return null;
}
else
{
return obj;
}
}
catch (system.data.sqlclient.sqlexception e)
{
connection.close();
throw e;
}
}
}
}
///
/// 执行查询语句,返回sqldatareader ( 注意:调用该方法后,一定要对sqldatareader进行close )
///
/// 查询语句
/// sqldatareader
public static sqldatareader executereader(string strsql)
{
sqlconnection connection = new sqlconnection(connectionstring);
sqlcommand cmd = new sqlcommand(strsql, connection);
try
{
connection.open();
sqldatareader myreader = cmd.executereader(commandbehavior.closeconnection);
return myreader;
}
catch (system.data.sqlclient.sqlexception e)
{
throw e;
}
}
///
/// 执行查询语句,返回dataset
- 2楼网友:青尢
- 2021-03-14 09:09
需求不够具体
如需帮助可加好友私聊
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯