【问题标题】:connection string in code file [closed]代码文件中的连接字符串[关闭]
【发布时间】:2013-03-18 08:52:12
【问题描述】:

我使用下面的类文件来创建连接字符串

namespace connect
{
    public class connection
    {
        string constr = "Data Source=.\\MYSQLEXPRESS;AttachDbFilename=C:\\Users\\Kuldeep\\Documents\\Visual Studio 2010\\Projects\\final\\App_Data\\information1.mdf;Integrated Security=True;User Instance=True";   
        public string constring
        {

                get
                {    
                    return constr;
                }
            }
        }  
    }

我想在我的代码文件中这样调用这个字符串。

public partial class Account_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);

    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(string constring);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from User where UserName =@UserName and Password=@Password", con);
        cmd.Parameters.AddWithValue("@UserName",.Text);
        cmd.Parameters.AddWithValue("@Password", txtPWD.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Response.Redirect("Default.aspx");
        }
        else
        {
            RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
          //  ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
        }  
        con.Close();
    }
}

在代码文件中调用连接字符串的代码语法是什么??

【问题讨论】:

  • 将连接字符串等设置编译到 dll 中并不是一个好主意。最好为此使用 app.config 或 web.config。如果您确实想继续使用这种方法,请将类或属性设为静态并像这样调用它:connection.constring

标签: c# mysql visual-studio-2010


【解决方案1】:

我建议你使用静态类

public static class Connection
{

....

}

并调用此代码来调用您的连接

var connection = Connection.Constring;

对于你的情况:

SqlConnection con = new SqlConnection(Connection.Constring);

注意:

  1. 我建议你对你的类和属性使用大写命名

  2. 建议你使用 using block arround connection 和 command object

链接:http://msdn.microsoft.com/fr-fr/library/system.data.sqlclient.sqlconnection(v=vs.80).aspx

  1. 另一个关于使用ConfigurationManager class外部化字符串连接的链接

链接:http://msdn.microsoft.com/fr-fr/library/system.configuration.configurationmanager.connectionstrings(v=vs.80).aspx

【讨论】:

  • 我刚试过..但没有用..
  • 只需验证您的类和方法的情况
  • 谢谢你..@frederique.. 但现在我要另辟蹊径.. 使用配置文件..
  • 很高兴为您提供帮助 这是最佳实践 Patel,是的,您可以使用 ConfigurationManager 类
  • 阅读此链接 Patel 它也很有趣:msdn.microsoft.com/fr-fr/library/…
【解决方案2】:

我建议您使用 App.config 文件来设置您的数据库凭据。

App.config 使用步骤

1.首先添加一个xml文件的App.config文件

2.将下面给出的代码添加到app.config文件中

 <

?xml version="1.0"?>
        <configuration>
          <appSettings>
            <add key="ConnectionString" value="Data Source=.\\MYSQLEXPRESS;AttachDbFilename=C:\\Users\\Kuldeep\\Documents\\Visual Studio 2010\\Projects\\final\\App_Data\\information1.mdf;Integrated Security=True;User Instance=True;"/>
          </appSettings>
         </configuration>

3.添加命名空间“using System.Configuration;” ,如果命名空间不可用,你应该添加 dll “System.Configuration”,然后使用下面的代码

 string con = ConfigurationSettings.AppSettings["ConnectionString"];     

【讨论】:

  • 是的,我现在使用它..就像这样..
  • 好..ie也是添加连接字符串的方式之一
猜你喜欢
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-18
  • 2015-02-25
相关资源
最近更新 更多