如何从 web.config 添加和访问连接字符串
我想添加连接字符串来连接到 web.config 文件中定义的 mysql,并在我的 C# 代码中访问相同的连接,我该怎么做?
这是我的代码示例,在单击按钮连接到数据库后运行。
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在所有这个答案中
static
缺少导入此命名空间:
获取连接字符串:
In all this answer
static
is missingimport this namespace:
Get connection string:
网络配置
aspx页面
web config
aspx page
的 ConnectionStrings 属性
如果您知道连接字符串名称,则可以使用
ConfigurationManager
类其中,您的 web.config 将包含名为
ConnectionStringName
的连接字符串If you know the connection string name you can use the ConnectionStrings property of the
ConfigurationManager
classE.g.
Where, your web.config would contain a connectionstring named
ConnectionStringName
以下是为 SQL Server 创建连接字符串并将其添加到 web.config 的示例
您可以将 sql 连接更改为 OleDb 连接
希望这将是完整的
Here is an Example of Creating and adding connectionString to the web.config for SQL Server
you can change the sql Connection to OleDb connection
Hope this will be use full
在网络配置中:
在视图中:
in web config:
in view: