CodePudding user response:
You all know that connect to the database has a problem, then you can see the database connection string, rightCodePudding user response:
You have a problem is to know that links database, but can't find where there is a problem is a headacheCodePudding user response:
Give your connection string configuration hairCodePudding user response:
Is your data is null, the breakpoint inside look, hiding inside the code to give you specific errorCodePudding user response:
Sqlhelper
using System;
using System.Data;
using System.Data.SqlClient;
Using System. The Configuration;
Using System. The Web;
Using System. Web. Security;
Using System. Web. UI.
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
///
///SqlHelper summary description
///
Public class SqlHelper
{
Public static readonly string connstring=ConfigurationManager. ConnectionStrings [r]. "ConnectionString" ConnectionString;
Private SqlConnection con;
Private SqlCommand CMD.
Private SqlDataAdapter sda.
Private SqlDataReader SDRS.
Private DataSet ds;
Private DataView dv.
Public static SqlConnection getConnection ()//defined as static, it is very important!
{
Return new SqlConnection (connstring);
}
///
///open the database connection
///
Public void OpenDataBase ()
{
SqlConnection con=new SqlConnection (connstring);
con.Open();
}
///
///close the database connection
///
Public void CloseDataBase ()
{
con.Close();
Con. The Dispose ();
}
///
///returns the DataSet data set
///
///
///
Public DataSet GetDs (string SqlStr, string TableName)
{
OpenDataBase ();
Sda=new SqlDataAdapter (SqlStr, con);
Ds=new DataSet ();
Sda. The Fill (ds, TableName);
CloseDataBase ();
Return the ds;
}
Public DataView GetDv (string SqlStr)
{
OpenDataBase ();
Sda=new SqlDataAdapter (SqlStr, con);
Ds=new DataSet ();
Sda. The Fill (ds);
Dv=ds. Tables [0]. DefaultView;
CloseDataBase ();
Return dv;
}
///
///returns the Datareader
///
///
///
Public SqlDataReader GetDataReader (string Sqlstr)
{
OpenDataBase ();
CMD=new SqlCommand (Sqlstr, con);
The SDR=CMD. ExecuteReader (System.Data.Com mandBehavior. CloseConnection);
Return the SDR.
}
///
///executing Sql statements method has no return value
///
///
Public void RunSql (string SqlStr)
{
OpenDataBase ();
CMD=new SqlCommand (SqlStr, con);
cmd.ExecuteNonQuery();
CloseDataBase ();
}
///
///returns the first line of the query results first
///
///
///
Public string ReturnSql (string SqlStr)
{
OpenDataBase ();
String ReturnSql="";
Try
{
CMD=new SqlCommand (SqlStr, con);
ReturnSql=CMD. ExecuteScalar (). The ToString ();
}
Catch {}
CloseDataBase ();
Return ReturnSql;
}
Public void DataCom (string SQLSTR)
{
OpenDataBase ();
SqlCommand sqlcom=new SqlCommand (SQLSTR, con);
Sqlcom. ExecuteNonQuery ();
CloseDataBase ();
}
Public void gvDataBind (the GridView gv, string SQLSTR)
{
OpenDataBase ();
SqlDataAdapter myda=new SqlDataAdapter (SQLSTR, con);
The DataSet myds=new DataSet ();
Myda. The Fill (myds);
Gv. The DataSource=myds;
Gv. DataBind ();
CloseDataBase ();
}
Public void dlDataBind (DataList dl, string SQLSTR)
{
OpenDataBase ();
SqlDataAdapter myda=new SqlDataAdapter (SQLSTR, con);
The DataSet myds=new DataSet ();
Myda. The Fill (myds);
Dl. The DataSource=myds;
Dl. DataBind ();
CloseDataBase ();
}
Public void ddlDataBind (DropDownList DDL, string SQLSTR, string DVF)
{
OpenDataBase ();
SqlDataAdapter myda=new SqlDataAdapter (SQLSTR, con);
The DataSet myds=new DataSet ();
Myda. The Fill (myds);
DDL. The DataSource=myds;
DDL. DataValueField=DVF;
DDL. DataBind ();
CloseDataBase ();
}
}