连接SQL数据库时出现问题 [英] Problem with connecting with SQL Database

查看:99
本文介绍了连接SQL数据库时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我的sql数据库连接有问题。我使用了本文中的代码:初学者访问SQL Server的指南通过C# [ ^ ]。



当我点击按钮进行连接时,编译器会向我显示如下错误:

System.Data.dll中出现类型为System.ArgumentException的未处理异常



附加信息:无效键值集成安全性




我第一次尝试,我不知道如何解决它。



这是我的代码,在按钮下:

Hi!

I have a problem with connection to my sql database. I used a code from this article: Beginners guide to accessing SQL Server through C#[^].

When im clicking the button to connect, then compilator is showing me an error like: "
An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: invalid key value „integrated security
"."

Im trying it first time, and i rly dont know how to fix it.

This is my code, that is under button:

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection myConnection = new SqlConnection("user id=root;" + "password=;" + "server=localhost;" + "Trusted_Connection=no" + "database=drzewo;" + "connection timeout=30");
            try
            {
                myConnection.Open();
                
            }
            catch (System.Data.SqlClient.SqlException)
            {
                MessageBox.Show("Nie połączono!", "Error!");
            }
        }

推荐答案

尝试使用Server Explorer窗格在VS中设置连接:

1)打开服务器资源管理器。

2)右键单击数据连接并选择添加连接

3)在随后的对话框中,选择您的数据源和数据库,指定安全信息,然后按测试连接按钮。

4)当连接工作时,按确定

5)突出显示数据库在服务器资源管理器窗格中,然后查看属性窗格。将显示连接字符串的工作示例,您可以将其复制并粘贴到您的应用程序或配置文件中。
Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.


使用SqlConnectionStringBuilder创建连接字符串。
Use a SqlConnectionStringBuilder to create your connectionstring.


这篇关于连接SQL数据库时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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