新手求教,B/S模式,用C#实现对SQL的连接并实现增删改查,求代码
3个回答
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace NewClient
{
class DBHelper
{
static string str = "data source=.;initial catalog=你的数据库名;integrated security=true";
public SqlConnection con = new SqlConnection(str);
/// <summary>
/// 查询单个值
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public object Scalar(string sql)
{
object o = null;
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
o = cmd.ExecuteScalar();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
return o;
}
/// <summary>
/// 查询多个值
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public SqlDataReader Reader(string sql)
{
SqlDataReader o = null;
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
o = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception)
{
throw;
}
return o;
}
/// <summary>
/// 增删改
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int NonQuery(string sql)
{
int o = -1;
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
o = cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
return o;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace NewClient
{
class DBHelper
{
static string str = "data source=.;initial catalog=你的数据库名;integrated security=true";
public SqlConnection con = new SqlConnection(str);
/// <summary>
/// 查询单个值
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public object Scalar(string sql)
{
object o = null;
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
o = cmd.ExecuteScalar();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
return o;
}
/// <summary>
/// 查询多个值
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public SqlDataReader Reader(string sql)
{
SqlDataReader o = null;
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
o = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception)
{
throw;
}
return o;
}
/// <summary>
/// 增删改
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int NonQuery(string sql)
{
int o = -1;
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
o = cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
return o;
}
}
}
展开全部
c#里B/S和C/S模式连接和操作数据可的代码相同,都用的ADO.Net。你去下ADO.Net的视频看吧,很简单
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上 51aspx 去下吧 一大把 我学习的时候也在上面下 的案列的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询