SQlHelper数据库连接
Posted iDennis
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
以上是关于SQlHelper数据库连接的主要内容,如果未能解决你的问题,请参考以下文章