1个回答
展开全部
数据库连接方法:
#region sql语句部分
/// <summary>
/// 执行sql语句返回DataTable
/// </summary>
/// <param name="sql">安全的sql语句</param>
/// <returns>根据sql语句得到所有记录</returns>
public static DataTable GetDataTable(string sql)
{
using (SqlConnection conn = new SqlConnection(Connstr))
{
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
读取数据
#region 读取数据
/// <summary>
/// 读取数据
/// </summary>
/// <returns>返回结果</returns>
public static string ExceSqlText()
{
string sqlText = "select name form tablename";
string dname = "";
try
{
DataTable dt = DBHelper.GetDataTable(sqlText);
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["name"] != DBNull.Value)
{
dname = dt.Rows[0]["name"].ToString();
}
return dname;
}
return null;
}
catch (Exception e)
{
return null;
}
}
#endregion
前台代码:
<asp:TextBox ID="txtString" runat="server"></TextBox>
后台代码:
this.txtString.Text = ExceSqlText() != null ? ExceSqlText() : "没有查询到任何数据";
#region sql语句部分
/// <summary>
/// 执行sql语句返回DataTable
/// </summary>
/// <param name="sql">安全的sql语句</param>
/// <returns>根据sql语句得到所有记录</returns>
public static DataTable GetDataTable(string sql)
{
using (SqlConnection conn = new SqlConnection(Connstr))
{
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
读取数据
#region 读取数据
/// <summary>
/// 读取数据
/// </summary>
/// <returns>返回结果</returns>
public static string ExceSqlText()
{
string sqlText = "select name form tablename";
string dname = "";
try
{
DataTable dt = DBHelper.GetDataTable(sqlText);
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["name"] != DBNull.Value)
{
dname = dt.Rows[0]["name"].ToString();
}
return dname;
}
return null;
}
catch (Exception e)
{
return null;
}
}
#endregion
前台代码:
<asp:TextBox ID="txtString" runat="server"></TextBox>
后台代码:
this.txtString.Text = ExceSqlText() != null ? ExceSqlText() : "没有查询到任何数据";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询