跪求C#增删改查代码。
就这点分了。各位C#大侠能指教下吗?我在学校专门学习..NET的。c#代码实现数据库的增删改查和.NET有什么区别。求源代码。求指教。C#的大神下。帮帮忙吧。感激不尽。...
就这点分了。 各位C#大侠能指教下吗? 我在学校专门学习..NET的。 c#代码实现数据库的增删改查和.NET有什么区别。 求源代码。求指教。 C#的大神下。 帮帮忙吧。 感激不尽。
展开
4个回答
展开全部
楼主你好: 下面代码 应该是你要的: using System;using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace WindowsApplication6{
public static class DBManager
{
private static string connectionstring = "Data source=(local)\\sqlexpress;initial catalog=pubs;integrated security=true;pooling=false;";
/// <summary>
/// 单条执行SQL
/// </summary>
/// <param name="sql">你的update或者delete或者insert语句</param>
/// <returns></returns>
public static int ExecuteSingleSql(string sql)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
try
{
SqlCommand cmd = new SqlCommand(sql, cnn);
cnn.Open();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
}
}
/// <summary>
/// 多条执行
/// </summary>
/// <param name="list"></param>
public static void ExecuteSqlTran(ArrayList list)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
cnn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
SqlTransaction tr = cnn.BeginTransaction(); cmd.Transaction = tr;
try
{
for (int n = 0; n < list.Count; n++)
{
string sql = list[n].ToString();
if (sql.Trim().Length > 0)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
}
tr.Commit();
}
catch (Exception ex)
{
tr.Rollback();
throw new Exception(ex.Message);
}
}
}
public static void ExecuteSqlTran2(ArrayList list) {
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
SqlTransaction tr = cnn.BeginTransaction(); cmd.Transaction = tr;
try {
for (int i = 0; i < list.Count; i++)
{
string sql = list[i].ToString();
if (sql.Trim().Length > 1)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
} tr.Commit();
} catch (Exception ex)
{
tr.Rollback();
throw new Exception(ex.Message);
}
}
}
/// <summary> /// 执行sql的反应行数
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int HasData(string sql)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
using (SqlCommand cmd = new SqlCommand(sql, cnn)) {
try {
cnn.Open();
return (Int32)cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex; }
}
}
}
/// <summary> /// 查询
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataSet GetDataSet(string sql)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
try {
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, cnn);
cnn.Open();
da.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
}
}}
希望对你有帮主,望采纳,谢谢
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace WindowsApplication6{
public static class DBManager
{
private static string connectionstring = "Data source=(local)\\sqlexpress;initial catalog=pubs;integrated security=true;pooling=false;";
/// <summary>
/// 单条执行SQL
/// </summary>
/// <param name="sql">你的update或者delete或者insert语句</param>
/// <returns></returns>
public static int ExecuteSingleSql(string sql)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
try
{
SqlCommand cmd = new SqlCommand(sql, cnn);
cnn.Open();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
}
}
/// <summary>
/// 多条执行
/// </summary>
/// <param name="list"></param>
public static void ExecuteSqlTran(ArrayList list)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
cnn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
SqlTransaction tr = cnn.BeginTransaction(); cmd.Transaction = tr;
try
{
for (int n = 0; n < list.Count; n++)
{
string sql = list[n].ToString();
if (sql.Trim().Length > 0)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
}
tr.Commit();
}
catch (Exception ex)
{
tr.Rollback();
throw new Exception(ex.Message);
}
}
}
public static void ExecuteSqlTran2(ArrayList list) {
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
SqlTransaction tr = cnn.BeginTransaction(); cmd.Transaction = tr;
try {
for (int i = 0; i < list.Count; i++)
{
string sql = list[i].ToString();
if (sql.Trim().Length > 1)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
} tr.Commit();
} catch (Exception ex)
{
tr.Rollback();
throw new Exception(ex.Message);
}
}
}
/// <summary> /// 执行sql的反应行数
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int HasData(string sql)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
using (SqlCommand cmd = new SqlCommand(sql, cnn)) {
try {
cnn.Open();
return (Int32)cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex; }
}
}
}
/// <summary> /// 查询
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataSet GetDataSet(string sql)
{
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
try {
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, cnn);
cnn.Open();
da.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
}
}}
希望对你有帮主,望采纳,谢谢
展开全部
.NET是平台,C#是只是其中的一种主流语言。首先你得知道t-sql语句,得会用t_sql语句会增删查改。然后你还要知道基础的C#语法。这些你都懂了再去搜索下ADO.net操作数据库,这也不是一两句话能说的完的,不会太难。祝你好运
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没什么区别 会.net的就会c#的
追问
求代码。 可以发到我邮箱519538508@qq.com 吗? 谢谢。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
51aspx.com 很多下载
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询