SQlHelper数据库连接
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>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步