SQlHelper数据库连接:
try { SQLHelper sqlhelper = new SQLHelper(); //SqlDataSource sql = new SqlDataSource(); //sql.DataSourceMode = SqlDataSourceMode.DataSet; DataSet ResultSet = new DataSet(); ResultSet.Merge(sqlhelper.RunQuery_getStaffName("SELECT [StaffNo], [ChineseName] FROM [VStaffNameSection] where staffno='" + txtStaffNo.Text + "'")); if (ResultSet.Tables[0].Rows.Count == 0) { Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script languge='javascript'>alert('工号不存在,请重新输入,谢谢!');</script>"); e.Cancel = true; return; } } catch (Exception ex) { throw ex; }
SQLHelper.cs
public class SQLHelper { public DataSet RunQuery_getStaffName(string QueryString) { System.Data.SqlClient.SqlConnection DBConnection = null; DBConnection = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["webConnectionString_Dept"].ToString()); DBConnection.Open(); SqlDataAdapter DBAdapter = default(SqlDataAdapter); DataSet ResultsDataSet = new DataSet(); try { DBAdapter = new SqlDataAdapter(QueryString, DBConnection); DBAdapter.Fill(ResultsDataSet); DBConnection.Close(); } catch (Exception ex) { if (DBConnection.State == ConnectionState.Open) { DBConnection.Close(); } } return ResultsDataSet; } }
Web.config
<connectionStrings> <add name="webConnectionString_Dept" connectionString="Data Source=XXX;Initial Catalog=XXX;Persist Security Info=True;User ID=XXX;Password=XXX" providerName="System.Data.SqlClient" /> </connectionStrings>