Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我用c连接SQL Server数据库的代码有什么错误#_C#_Sql Server - Fatal编程技术网

C# 我用c连接SQL Server数据库的代码有什么错误#

C# 我用c连接SQL Server数据库的代码有什么错误#,c#,sql-server,C#,Sql Server,请参阅下面显示的代码-错误在哪里或我犯了什么错误?控制台没有显示任何内容,只是一个黑色空白 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace sql_connect { class Program { st

请参阅下面显示的代码-错误在哪里或我犯了什么错误?控制台没有显示任何内容,只是一个黑色空白

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace sql_connect
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection myConnection = new SqlConnection("Database=Db_test;Server=.\\SQLEXPRESS;Integrated Security=True;connect timeout = 30");

            try
            {
                myConnection.Open();
                Console.WriteLine("Well Done");
            }
            catch (SqlException ex)
            {
                Console.WriteLine("You failed!" + ex.Message);
            }
        }
    }
}

您是否在SQLExpress上创建了Db_测试?

首先检查数据库与UDL文件的连接。更多信息。

如果您没有收到任何错误,可能是连接有问题。你可能很快就会超时。请检查是否可以通过其他工具(例如SQLServerManagementStudio)访问此数据库。可能是连接字符串中的输入错误


旁注:将sqlconnection放在using中,以使其在停止使用后始终处于释放状态

您收到了什么错误消息?是否尝试调试它?还有一件事,尝试添加catch(Exception e),是的,我同意您已经添加了SqlException,请通过捕获而不是sql Exception进行尝试。