Chinaunix首页 | 论坛 | 博客
  • 博客访问: 446524
  • 博文数量: 145
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1060
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-22 11:52
个人简介

专注计算机技术: Linux Android 云计算 虚拟化 网络

文章分类

全部博文(145)

文章存档

2016年(3)

2015年(21)

2014年(75)

2013年(46)

我的朋友

分类: C#/.net

2014-05-15 17:03:09



 private static string CONN = @"Data Source=\SQLEXPRESS; DataBase=tb_taizhan;Integrated Security=true";

执行查询


dataset
public static DataSet ExecuteSelectSql(string strsql)
{
SqlConnection sqlconn = new SqlConnection(CONN);
SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlconn);
DataSet ds = new DataSet();
try
{
sda.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw new Exception("执行SQL语句出错:\r\n" + strsql + "\r\n" + ex.ToString());

}
finally
{
sqlconn.Close();
}


}

///
/// 执行更新
///

///
/// int
public static int ExecuteUpdateSql(string strsql)
{
SqlConnection sqlconn = new SqlConnection(CONN);
SqlCommand cmd = new SqlCommand(strsql);
cmd.Connection = sqlconn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strsql;
int rec = -1;
try
{
sqlconn.Open();
rec = cmd.ExecuteNonQuery();
return rec;
}
catch (Exception ex)
{
throw new Exception("执行SQL语句出错:\r\n" + strsql + "\r\n" + ex.ToString());

}
finally
{
sqlconn.Close();
}


}

///
/// 执行删除
///

///
/// int
public static int ExecuteDelSql(string strsql)
{
SqlConnection sqlconn = new SqlConnection(CONN);
SqlCommand cmd = new SqlCommand(strsql, sqlconn);
int rec = -1;
try
{
sqlconn.Open();
rec = cmd.ExecuteNonQuery();
return rec;
}
catch (Exception ex)
{
throw new Exception("执行SQL语句出错:\r\n" + strsql + "\r\n" + ex.ToString());

}
finally
{
sqlconn.Close();
}


}

///
/// 执行Insert语句
///

///
///
public static int ExecuteInsertSql(string strSql)
{
SqlConnection conn = new SqlConnection(CONN);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = strSql;
int ret = -1;
try
{
conn.Open();
ret = comm.ExecuteNonQuery();

return ret;
}
catch (Exception ex)
{
throw new Exception("执行SQL出现错误:\r\n" + strSql + "\r\n" + ex.ToString());
}
finally
{
conn.Close();
}


//throw new NotImplementedException();
}

public static Object ExecuteScalar(string strSql)
{
SqlConnection conn = new SqlConnection(CONN);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = strSql;

try
{
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
return dr[0];
}
else
{
return null;
}
}
catch (Exception ex)
{
throw new Exception("执行SQL出现错误:\r\n" + strSql + "\r\n" + ex.ToString());
}
finally
{
conn.Close();
}


//throw new NotImplementedException();
}
阅读(777) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~