Cannot attach the file 'C:Users ayeDocuments(LocalDB)MSSQLLocalDB.mdf' as database 'D:DIYDMS-

第一次C#连接数据库

希望在控制台界面上看到一个 success 就说明连接上了

这是我的代码

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

namespace DMSconfig
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection(DMSconfig.Properties.Settings.Default.DMSstring);
            try
            {
                conn.Open();
                System.Threading.Thread.Sleep(3000);
                Console.WriteLine("success");
            }
            catch(Exception ex)
            {
                Console.Write(ex.Message);
            }
            finally
            {
                if(conn.State==System.Data.ConnectionState.Open)
                {
                    conn.Close();
                }
                Console.WriteLine("quit");
            }
            Console.Read();
        }
    }
}

但是很不幸:

这是为什么呢?

我在stackoverflow上找到了和我有一样疑问的人的问题,并找到了正确答案。

于是我也找到了对应字段并删除。

然后再运行一次:

原文地址:https://www.cnblogs.com/luckyraye/p/7922280.html