直接在表单的load事件中建立SQL Server连接。 [英] Establishing a SQL Server connection directly in the load event of a form.

查看:80
本文介绍了直接在表单的load事件中建立SQL Server连接。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#2.0和SQL Server 2005开发了一个Windows应用程序。在这个应用程序中,我使用一个表单建立SQL Server连接,其中我使用了三个文本框,一个组合框和两个按钮控件这样

服务器名称: - textBox1

身份验证: - comboBox1

用户ID: - textBox2

密码: - textBox3

登录: - button1取消: - 按钮2



代码示例: -

I developed a Windows Application using C# 2.0 and SQL Server 2005. In this application, I establish SQL Server connection using a form in which I used three textboxes, one combobox and two buttons controls like this
Server Name :- textBox1
Authentication:- comboBox1
User ID:- textBox2
Password:- textBox3
Login:- button1 Cancel:-button2

Code Sample:-

private void button1_Click(object sender, EventArgs e)
        {
            Class1.cs = "Data Source= " + textBox1 + ";Initia Catalog=Kolkata_Dental;User ID=" + textBox2.Text + ";Password=" + textBox3.Text + "";
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = Class1.cs;
                    con.Open();
                    Class1.connection = true;
                    MessageBox.Show("Connection estabilished Successfully!!!");
                    con.Close();
                    Home_Page frm = new Home_Page();
                    frm.Show();
                    this.Hide();
        }





现在,我不想通过使用这些控件来建立SQL连接。我想直接在该表单的load事件中创建连接而不单击按钮控件。先生可以吗?请给我一个很好的解决方案。



Now, I don't want to establish the SQL connection by using those controls. I want to create the connection directly in load event of that form without clicking a button control. Is it possible Sir? Please give me good solution.

推荐答案

我不能完全确定你的问题是什么,但我猜你需要存储连接字符串一旦用户使其工作,并在表单加载时重新建立该字符串。幸运的是,这很简单:只需使用Settings工具。

有两种方法可以执行此操作:存储构成字符串的文本框值(这样您就可以将它们加载到文本框中)如果用户需要更改它或存储整个字符串 - 大多数系统都在整个字符串上工作,因为通常不会更改各个部分。原理是相同的。



在VS中打开项目,然后在解决方案资源管理器中打开属性下拉列表。

双击Settings.settings行。

在结果页面中,在名称列中添加合适的名称(例如strConnect)并离开其他所有内容为String,User和空白(除非你想要一个默认值,在这种情况下用连接字符串替换空白。

关闭页面(它可能会要求你保存,如果是的话,那就这样做。



在你的表单加载事件你可以通过

I'm not absolutely sure what your problem is with doing this, but I'm guessing that you need to store the connection string once the user has got it working, and re-establish that string when the form loads. Fortunately, this is pretty simple: just use the Settings facility.
There are two ways you might want to do this: store the textbox values that make up the string (so you can load them into the textboxes if the user needs to change it) or store the whole string - most systems work on the whole string as the individual parts are not normally changed often. The principle is the same either way.

Open your project in VS, and open the Properties drop down in the Solution Explorer.
Double click the "Settings.settings" line.
In the resulting page, add a suitable name in the "Name" column ("strConnect" for example) and leave everything else as "String", "User" and blank (unless you want a default value, in which case replace the blank with the connection string.
Close the page (it may ask you to save, if it does, do so.

In your form Load event you can know access the string via
string strConnect = Properties.Settings.Default.strConnect;

您使用相同的原则来存储各个部分,每个部分都有一个单独的设置。



如果这不是您想要做的,那么您需要更详细地解释。

You use the same principle to store the various parts, with a separate setting for each.

If this isn't what you are trying to do, then you need to explain in better detail.


你也可以使用注册表来存储连接字符串。看看这个 使用C#从注册表中读取,写入和删除 [ ^ ]。第一次需要使用默认值对其进行初始化,稍后您可以在程序中添加一些支持来编辑连接字符串。



出于安全考虑,它是在plaint-text中存储凭据不是一个好的做法/想法,看一看,它可能会有所帮助: 使用C#加密和解密数据 [ ^ ]
You could also use the registry for storing the connection string. Take a look at this Read, write and delete from registry with C#[^]. The first time you will need to initialize it with defaults, later on you may add some support in your program for editing the connection string.

For security reasons, it is not a good practice/idea to store credentials in plaint-text, take a look also to this, it could help: Encrypt and Decrypt Data with C#[^]


是的你可以使用,



但是您应该使用相应的文本框分配值。







textBox1.Text =服务器名称



textBox1.Text:用户ID:

textBox3密码
Yes You can use,

But you should assign the values with the corresponding textboxes.

Like

textBox1.Text=Server Name

textBox1.Text :User ID:
textBox3 Password


这篇关于直接在表单的load事件中建立SQL Server连接。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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