我一直保存到注册表中的信息,这些信息是在早期创建的表单上的文本框中键入的。打开表单后,我想显示所有保存到文本框。当我尝试运行下面的代码时,它返回空值。 可能是什么问题?从文本框(C#)获取价值
代码:
SQLSERVER = textBox1.Text;
SQLDATABASE = textBox2.Text;
SQLUSER = textBox3.Text;
SQLPASS = textBox4.Text;
try
{
SqlConnection Baglanti = new SqlConnection("Data Source='" + SQLSERVER + "'; Initial Catalog='" + SQLDATABASE + "'; User id='" + SQLUSER + "'; Password='" + SQLPASS + "';");
Baglanti.Open();
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software", true);
if (key != null)
{
RegistryKey key2 = key.CreateSubKey("BilkerSoft");
key.SetValue("SQLSERVER", SQLSERVER, RegistryValueKind.String);
Registry.CurrentUser.CreateSubKey("BilkerSoft").SetValue("SQLSERVER", SQLSERVER);
Registry.CurrentUser.CreateSubKey("BilkerSoft").SetValue("DATABASE", SQLDATABASE);
Registry.CurrentUser.CreateSubKey("BilkerSoft").SetValue("USER", SQLUSER);
Registry.CurrentUser.CreateSubKey("BilkerSoft").SetValue("PASSWORD", SQLPASS);
}
}
catch (Exception ex)
{
MessageBox.Show("Hata oluştu:'" + ex.Message + "'");
}
RegistryKey key1 = Registry.CurrentUser.OpenSubKey("BilkerSoft",true);
try
{
if (key1 != null)
{
key1.SetValue("SQLSERVER", SQLSERVER, RegistryValueKind.String);
Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("SQLSERVER", SQLSERVER);
Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("DATABASE", SQLDATABASE);
Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("USER", SQLUSER);
Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("PASSWORD", SQLPASS);
}
Baglanti = new SqlConnection("Data Source='" + SQLSERVER + "';Initial Catalog='" + SQLDATABASE + "';User id='" + SQLUSER + "';Password='" + SQLPASS + "'");
Baglanti.Open();
Baglanti.Close();
MessageBox.Show("Kayıt Başarılı");
}
catch (Exception ex)
{
MessageBox.Show("Hata oluştu:'" + ex.Message + "'");
}
}
private void Form1_Load(object sender, EventArgs e)
{
RegistryKey key2 = Registry.CurrentUser.OpenSubKey("BilkerSoft", true);
textBox1.Text = Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("SQLSERVER", SQLSERVER).ToString();
textBox2.Text = Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("DATABASE", SQLDATABASE).ToString();
textBox3.Text = Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("USER", SQLUSER).ToString();
textBox4.Text = Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("PASSWORD", SQLPASS).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
您meen textBox1.Text == NULL? –
哪一个是空的?键? key1的?他们全部? – itsmatt
@ itsmatt-key1为空。 – Selo