然后在Webform_ aspx cs里面获取连接字符串 要添加如下引用
复制代码 代码如下: using System Configurationusing System Datausing System Data SqlClient 复制代码 代码如下: SqlConnection conprotected void Page_Load(object sender EventArgs e) { ConnectDB()} private void ConnectDB() { string ConString = ConfigurationManager ConnectionStrings["ConnectionString"] ConnectionStringcon = new SqlConnection(ConString)con Open()SqlCommand = new SqlCommand()SqlDataReader sdrstring sqlstr = "select * from item"CommandText = sqlstrConnection = consdr = ExecuteReader()while (sdr Read()) { Response Write(sdr["字段名"] ToString()+"</br>")} sdr Close()sdr = null} lishixinzhi/Article/program/net/201311/141061、在应用程序中的配置节中直接添加这个配置节要找到自己的连接数据库的配置节就直接在通过EF进行创建实体模型。
2、打开连接到数据库提供方终结点的应用程序的应用程序配置文件即可。
#region 建立MySql数据库连接/// <summary>
/// 建立数据库连接.
/// </summary>
/// <returns>返回MySqlConnection对象</returns>
public MySqlConnection getmysqlcon()
{
//http://sosoft.cnblogs.com/
string M_str_sqlcon = "server=localhostuser id=rootpassword=rootdatabase=abc" //根据自己的设置
MySqlConnection myCon = new MySqlConnection(M_str_sqlcon)
return myCon
}
#endregion
#region 执行MySqlCommand命令
/// <summary>
/// 执行MySqlCommand
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
public void getmysqlcom(string M_str_sqlstr)
{
MySqlConnection mysqlcon = this.getmysqlcon()
mysqlcon.Open()
MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon)
mysqlcom.ExecuteNonQuery()
mysqlcom.Dispose()
mysqlcon.Close()
mysqlcon.Dispose()
}
#endregion
#region 创建MySqlDataReader对象
/// <summary>
/// 创建一个MySqlDataReader对象
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
/// <returns>返回MySqlDataReader对象</returns>
public MySqlDataReader getmysqlread(string M_str_sqlstr)
{
MySqlConnection mysqlcon = this.getmysqlcon()
MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon)
mysqlcon.Open()
MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection)
return mysqlread
}
#endregion
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)