简体   繁体   English

回收 Windows Forms C# 中的连接字符串

[英]Recycling Connection String In Windows Forms C#

I'm trying to reuse a connection string created from a DataGridView.我正在尝试重用从 DataGridView 创建的连接字符串。 I can't seem to translate the value in the Settings.settings file to a usable connection string.我似乎无法将 Settings.settings 文件中的值转换为可用的连接字符串。 Any ideas of what the Connection_String SHOULD look like?关于 Connection_String应该是什么样子的任何想法?

Error:错误:

Format of the initialization string does not conform to specification starting at index 0.初始化字符串的格式不符合从索引 0 开始的规范。

Code:代码:

 // This fills the gridview
                this.loanacctTableAdapter.FillBy(this.innovate_Loan_ServicingDataSet.loanacct, ((decimal)(System.Convert.ChangeType(acctrefnoToolStripTextBox.Text, typeof(decimal)))));

// This tries to use the same connection
 Connection_String = SpecialSetting.ConnectionString.ToString();
 Command = "SELECT acctrefno FROM loanacct WHERE acctrefno = " + AcctrefnoToolStripTextBox.Text + "";
SqlConnection Conn = new SqlConnection(Connection_String);
SqlCommand Comm1 = new SqlCommand(Command, Conn);
Conn.Open();
SqlDataReader DR1 = Comm1.ExecuteReader();
if (DR1.Read())
   {
   textBox1.Text = DR1.GetValue(0).ToString();
   textBox1.ReadOnly = true;
   }
Conn.Close();

Settings.Designer.cs设置.Designer.cs

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
        [global::System.Configuration.DefaultSettingValueAttribute("Data Source=ILSVUPGRADE01;Initial Catalog=Innovate_Loan_Servicing;Integrated Secu" +
            "rity=True")]
        public string Innovate_Loan_ServicingConnectionString {
            get {
                return ((string)(this["Innovate_Loan_ServicingConnectionString"]));

ConnectionString is simply a String. ConnectionString 只是一个字符串。 I suggest to use the SqlConnectionStringBuilder Class.我建议使用SqlConnectionStringBuilder Class。 It makes it much easier to create a valid connection string.它使创建有效的连接字符串变得更加容易。

You can get the full ConnectionString then with .ToString()您可以使用.ToString()获得完整的 ConnectionString

The solution ended up being the difference between Windows Forms and ASP.NET.该解决方案最终成为 Windows Forms 和 ASP.NET 之间的差异。 I needed to find the configuration in the Settings.settings file and drill down.我需要在 Settings.settings 文件中找到配置并向下钻取。 Windows forms stores the connection string as "Properties.Settings.Default. name of connection here.ToString(); The command and connection can then be set simply. ASP.NET sets the connection string under ConfigurationSettings.AppSettings("myConnectionString"). Windows forms 将连接字符串存储为“Properties.Settings.Default.name of connection here.ToString(); 然后可以简单地设置命令和连接。ASP.NET 设置连接字符串”在 ConfigurationSettings.AppSettings 下。

My code ended up looking like this.我的代码最终看起来像这样。

string Connection_String = Properties.Settings.Default.Innovate_Loan_ServicingConnectionString.ToString();
string Command = "SELECT acctrefno, loan_number, [shortname], [loan_group_no] FROM loanacct WHERE acctrefno = " + acctrefnoToolStripTextBox.Text + "";
SqlConnection Conn = new SqlConnection(Connection_String);
SqlCommand Comm1 = new SqlCommand(Command, Conn);
Conn.Open();
SqlDataReader DR1 = Comm1.ExecuteReader();
if (DR1.Read())

etc.等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM