如何添加和访问web.config中的连接字符串 [英] How to add and access connection string from web.config

查看:154
本文介绍了如何添加和访问web.config中的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加连接字符串连接到MySQL是定义web.config文件,访问我的C#code相同的连接我该怎么做呢?

下面是我的code样品,一个按钮的onclick运行后连接到数据库。

 保护无效Button2_Click(对象发件人,EventArgs的发送)
    {
        字符串= DropDownList1.SelectedItem.Value;
        串B = DropDownList3.SelectedItem.Value.PadLeft(3,'0');
        字符串C = TextBox2.Text.PadLeft(5,'0')的ToString()。
        STRING D = TextBox3.Text.ToString();
        串位= A + B + C + D;
        尝试
        {
         的myconn =新OdbcConnection(驱动程序= {MySQL的ODBC驱动程序3.51};服务器=本地主机;数据库=测试用例;用户=根;密码=根;选项= 3;);
         myConn.Open();
            // **
            字符串SQL =?从哪里testcase.main = reg_no选择*;
            // **
            的OdbcCommand CMD =新的OdbcCommand(SQL,的myconn);
            // **
            cmd.Parameters.AddWithValue(,数字?);
            MyReader = cmd.ExecuteReader();
            // **
            而(MyReader.Read())
            {
                。字符串型F = MyReader [pet_name]的ToString();
                。字符串G = MyReader [RES_NAME]的ToString();                Label9.Visible = TRUE;
                Label9.Text = F;                Label10.Visible = TRUE;
                Label10.Text =VS;                //Label11.Visible = TRUE;
                Label11.Text =克;            }            MyReader.Close();
        }
        赶上(例外E1)
        {
            的Response.Write(e1.ToString());
        }
        最后
        {
            如果(MyReader = NULL&放大器;!&安培;!MyReader.IsClosed)
            {
                MyReader.Close();
            }            如果(的myconn = NULL&放大器;!&安培; myConn.State == ConnectionState.Open)
            {
                myConn.Close();
            }
        }


解决方案

如果您知道连接字符串名称可以使用的ConnectionStrings的 ConfigurationManager中类物业

例如

 使用System.Configuration;
。字符串的ConnectionString = ConfigurationManager.ConnectionStrings [的connectionStringName] ConnectionString.ToString();

在哪里,你的web.config将包含一个名为ConnectionString的的connectionStringName

I want to add connection string to connect to mysql that are define in web.config file and access the same connection in my C# code how can I do this?

Here is my code sample that run after onclick of a button to connect to database.

protected void Button2_Click(object sender, EventArgs e)
    {
        String a = DropDownList1.SelectedItem.Value;
        String b = DropDownList3.SelectedItem.Value.PadLeft(3, '0');      
        String c = TextBox2.Text.PadLeft(5,'0').ToString();
        String d = TextBox3.Text.ToString();
        String digit = a+ b  + c + d;
        try
        {
         myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=testcase;User=root;Password=root;Option=3;");
         myConn.Open();
            //**
            string sql = "select * from testcase.main where reg_no =?";            
            //**
            OdbcCommand cmd = new OdbcCommand(sql, myConn);            
            //**
            cmd.Parameters.AddWithValue("?", digit);
            MyReader = cmd.ExecuteReader();
            //**
            while (MyReader.Read())
            {
                String f = MyReader["pet_name"].ToString();
                String g = MyReader["res_name"].ToString();

                Label9.Visible = true;
                Label9.Text = f;

                Label10.Visible = true;
                Label10.Text = "VS";

                //Label11.Visible = true;
                Label11.Text = g;

            }

            MyReader.Close();
        }
        catch (Exception e1)
        {
            Response.Write(e1.ToString());
        }
        finally
        {
            if (MyReader != null && !MyReader.IsClosed)
            {
                MyReader.Close();   
            }

            if (myConn != null && myConn.State == ConnectionState.Open)
            {
                myConn.Close();
            }
        }

解决方案

If you know the connection string name you can use the ConnectionStrings property of the ConfigurationManager class

E.g.

using System.Configuration;
string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString.ToString();

Where, your web.config would contain a connectionstring named ConnectionStringName

这篇关于如何添加和访问web.config中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆