failed to connect to database using c#

时间:2022-05-07 07:41:00

I want to ask about connecting db with c#. I have read http://csharp.net-informations.com/data-providers/csharp-sql-server-connection.htm and written my code, but the connection doesn't seem to be working.

我想问一下将db与c#连接起来。我已经阅读了http://csharp.net-informations.com/data-providers/csharp-sql-server-connection.htm并编写了我的代码,但连接似乎没有用。

This is my code:

这是我的代码:

string connetionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
SqlDataReader dataReader;
connetionString = "Data Source=localhost;Initial Catalog=dbabc;User ID=admin;Password=qwerty";
sql = "UPDATE ppd,brg,cmp SET WHERE";
connection = new SqlConnection(connetionString);
try
{
    connection.Open();
    command = new SqlCommand(sql, connection);
    dataReader = command.ExecuteReader();
    while (dataReader.Read())
    {
        MessageBox.Show(dataReader.GetValue(0) + " - " + dataReader.GetValue(1) + " - " + dataReader.GetValue(2));
    }
    dataReader.Close();
    command.Dispose();
    connection.Close();
}
catch (Exception ex)
{
    MessageBox.Show("Can not open connection ! ");
}

Did I write something wrong? The error message shown always says "Can not open connection"!

我写错了吗?显示的错误消息始终显示“无法打开连接”!

4 个解决方案

#1


1  

The Exception thrown should be a SqlException. Check the ErrorCode on the SqlException and look it up here...

抛出的异常应该是SqlException。检查SqlException上的ErrorCode并在这里查找...

https://docs.microsoft.com/en-us/azure/sql-database/sql-database-develop-error-messages

Rather than catch(Exception ex) your code should do this...

而不是捕获(Exception ex),你的代码应该这样做......

try
{
    connection.Open();
    ....
    connection.Close();
}
catch (SqlException ex)
{
    MessageBox.Show($"Can not open connection ! ErrorCode: {ex.ErrorCode} Error: {ex.Message}");
}
catch (Exception ex)
{
    MessageBox.Show($"Can not open connection ! Error: {ex.Message}");
}

#2


0  

I think you should check server name

我认为你应该检查服务器名称

if you install SQL server (Standard) on your computer, server name named is (local) or .

如果在计算机上安装SQL Server(标准版),则服务器名称为(本地)或。

#3


0  

If you are using VS create a data source and test the connection string. Go to data> Add new data source. There is find your database server and create the correct string.

如果您使用VS创建数据源并测试连接字符串。转到数据>添加新数据源。找到您的数据库服务器并创建正确的字符串。

#4


0  

Your code is working. I can verify it. Please check your connection string and SQL query. Especially your SQL query. It seems to be wrong. Try to execute the query in Microsoft SQL Server Management Studio. If there is an error in your query, you can easily find it there.

你的代码正在运行。我可以验证它。请检查您的连接字符串和SQL查询。特别是你的SQL查询。这似乎是错的。尝试在Microsoft SQL Server Management Studio中执行查询。如果您的查询中存在错误,您可以在那里轻松找到它。

SQL Update Syntax

SQL更新语法

UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

Recommended Readings
https://www.tutorialspoint.com/sql/sql-update-query.htm
https://www.w3schools.com/sql/sql_update.asp

推荐读物https://www.tutorialspoint.com/sql/sql-update-query.htm https://www.w3schools.com/sql/sql_update.asp

To verify the connection string,

要验证连接字符串,

  1. Open the Server Explorer pane. ("View" menu, "Server Explorer" or use the key shortcut CTRL+Alt+S).
  2. 打开Server Explorer窗格。 (“查看”菜单,“服务器资源管理器”或使用快捷键CTRL + Alt + S)。

  3. Right-click on the Data Connections, Select Add connection and then press continue after selecting Microsft SQL Server as the Data source.
  4. 右键单击“数据连接”,选择“添加连接”,然后在选择“Microsft SQL Server”作为“数据源”后按“继续”。

  5. Select Refresh and click down arrow and then select your SQL server. If you cannot find it, enter the server name manually.
  6. 选择Refresh并单击向下箭头,然后选择您的SQL Server。如果找不到,请手动输入服务器名称。

  7. You may also have to enter the SQL server credentials.
  8. 您可能还必须输入SQL Server凭据。

  9. Select or enter your database name & then press Test Connection.
  10. 选择或输入数据库名称,然后按“测试连接”。

  11. If you see a Test connection succeeded message, then your SQL server is working.
  12. 如果您看到测试连接成功消息,那么您的SQL服务器正在运行。

  13. If you want, you can also get connection string by pressing Advanced.. button and then copying the connection string
  14. 如果需要,还可以通过按Advanced ..按钮然后复制连接字符串来获取连接字符串

failed to connect to database using c#

In addition, try to an add exception message to catch block. That way, you can easily find the error.

此外,尝试添加异常消息以捕获阻止。这样,您可以轻松找到错误。

MessageBox.Show("Can not open connection ! \n" + ex.Message);

#1


1  

The Exception thrown should be a SqlException. Check the ErrorCode on the SqlException and look it up here...

抛出的异常应该是SqlException。检查SqlException上的ErrorCode并在这里查找...

https://docs.microsoft.com/en-us/azure/sql-database/sql-database-develop-error-messages

Rather than catch(Exception ex) your code should do this...

而不是捕获(Exception ex),你的代码应该这样做......

try
{
    connection.Open();
    ....
    connection.Close();
}
catch (SqlException ex)
{
    MessageBox.Show($"Can not open connection ! ErrorCode: {ex.ErrorCode} Error: {ex.Message}");
}
catch (Exception ex)
{
    MessageBox.Show($"Can not open connection ! Error: {ex.Message}");
}

#2


0  

I think you should check server name

我认为你应该检查服务器名称

if you install SQL server (Standard) on your computer, server name named is (local) or .

如果在计算机上安装SQL Server(标准版),则服务器名称为(本地)或。

#3


0  

If you are using VS create a data source and test the connection string. Go to data> Add new data source. There is find your database server and create the correct string.

如果您使用VS创建数据源并测试连接字符串。转到数据>添加新数据源。找到您的数据库服务器并创建正确的字符串。

#4


0  

Your code is working. I can verify it. Please check your connection string and SQL query. Especially your SQL query. It seems to be wrong. Try to execute the query in Microsoft SQL Server Management Studio. If there is an error in your query, you can easily find it there.

你的代码正在运行。我可以验证它。请检查您的连接字符串和SQL查询。特别是你的SQL查询。这似乎是错的。尝试在Microsoft SQL Server Management Studio中执行查询。如果您的查询中存在错误,您可以在那里轻松找到它。

SQL Update Syntax

SQL更新语法

UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

Recommended Readings
https://www.tutorialspoint.com/sql/sql-update-query.htm
https://www.w3schools.com/sql/sql_update.asp

推荐读物https://www.tutorialspoint.com/sql/sql-update-query.htm https://www.w3schools.com/sql/sql_update.asp

To verify the connection string,

要验证连接字符串,

  1. Open the Server Explorer pane. ("View" menu, "Server Explorer" or use the key shortcut CTRL+Alt+S).
  2. 打开Server Explorer窗格。 (“查看”菜单,“服务器资源管理器”或使用快捷键CTRL + Alt + S)。

  3. Right-click on the Data Connections, Select Add connection and then press continue after selecting Microsft SQL Server as the Data source.
  4. 右键单击“数据连接”,选择“添加连接”,然后在选择“Microsft SQL Server”作为“数据源”后按“继续”。

  5. Select Refresh and click down arrow and then select your SQL server. If you cannot find it, enter the server name manually.
  6. 选择Refresh并单击向下箭头,然后选择您的SQL Server。如果找不到,请手动输入服务器名称。

  7. You may also have to enter the SQL server credentials.
  8. 您可能还必须输入SQL Server凭据。

  9. Select or enter your database name & then press Test Connection.
  10. 选择或输入数据库名称,然后按“测试连接”。

  11. If you see a Test connection succeeded message, then your SQL server is working.
  12. 如果您看到测试连接成功消息,那么您的SQL服务器正在运行。

  13. If you want, you can also get connection string by pressing Advanced.. button and then copying the connection string
  14. 如果需要,还可以通过按Advanced ..按钮然后复制连接字符串来获取连接字符串

failed to connect to database using c#

In addition, try to an add exception message to catch block. That way, you can easily find the error.

此外,尝试添加异常消息以捕获阻止。这样,您可以轻松找到错误。

MessageBox.Show("Can not open connection ! \n" + ex.Message);