C# 2010 Express + SQL Server 2008 Express - 连接 "Login failed"

标签 c# sql-server-2008 connection

我实际上正在使用 Visual C# Express 2010 开发一个 Windows 窗体应用程序,它将使用(读/写)来自 SQL Server 2008 Express 数据库的数据

我已经使用 SQL Server Management Studio (2008 Express) 创建了我的数据库, 我知道该实例名为 ATLELAG786576\SQLEXPRESS 我的数据库名为“TEST”

在 SQL Server Management Studio (2008 Express) 中查看我的数据库“TEST”属性: 在文件下,我是 (ATLE\bneveux) 数据库的所有者

在安全、登录、Mylogin (ATLE\bneveux) 下查看

  • 我的默认数据库是“TEST”
  • 服务器角色是“公共(public)”+“系统管理员”
  • 用户映射数据库“TEST”用户“dbo”默认架构“dbo”

在我的 C# 应用程序中

应用程序配置:

<?xml version="1.0" encoding="utf-8" ?> <configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="connectionStringTestDb"
            connectionString="Data Source=ATLELAG786576\SQLEXPRESS;Initial Catalog=D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf;Integrated Security=True;Connect Timeout=30;User Instance=False"
            providerName="System.Data.SqlClient" />
    </connectionStrings> </configuration>

dbConnection.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace SQLServerConnectionDemo
{
    class dbConnection
    {
        public static SqlConnection newCon;
        public static string connectionStringTestDb = ConfigurationManager.ConnectionStrings["connectionStringTestDb"].ConnectionString;

        public static SqlConnection GetConnection()
        {
            newCon = new SqlConnection(connectionStringTestDb);
            return newCon;
        }
    }
}

dbAccess.cs:

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

namespace SQLServerConnectionDemo
{
    class dbAccess
    {
        SqlConnection conn;
        public dbAccess()
        {
            conn = dbConnection.GetConnection();
        }

        //Method insert new in tblEmployees
        public void addEmployee(string Id, string Name, string Email)
        {
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            SqlCommand newCmd = conn.CreateCommand();
            newCmd.Connection = conn;
            newCmd.CommandType = CommandType.Text;
            newCmd.CommandText = "INSERT INTO tblEmployees VALUES ('"+ Id +"','"+ Name +"','"+ Email +"')";
            newCmd.ExecuteNonQuery();
        }

    }
}

在表单 formEmployeeAdd.cs 中:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SQLServerConnectionDemo
{
    public partial class formEmployeeAdd : Form
    {

        dbAccess access = new dbAccess();

        public formEmployeeAdd()
        {
            InitializeComponent();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            access.addEmployee(txtId.Text, txtName.Text, txtEmail.Text);
            MessageBox.Show("Data successfully added");
        }
    }
}

这里是我在尝试运行此过程时总是收到的错误消息:

System.Data.SqlClient.SqlException (0x80131904): Cannot open database "D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf" requested by the login. The login failed. Login failed for user 'ATLE\bneveux'.

请注意,我从来没有真正能够在 Visual C# 2010 Express 中添加我的数据源,因此我可以从 VS 管理数据库,我总是收到以下错误消息:

Unable to open the physical file "D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf". Operating system error 32: "32(Le processus ne peut pas accéder au fichier car ce fichier est utilisé par un autre processus.)". An attempt to attach an auto-named database for file D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

最佳答案

尝试替换

Initial Catalog=D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf

简单地

Initial Catalog=TEST

关于C# 2010 Express + SQL Server 2008 Express - 连接 "Login failed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13451828/

相关文章:

c# - 获取继承类型列表并不适用于所有类型

c# - 按字母顺序排列,Array.Sort() 效果不佳

sql - 如何在没有任何备份的情况下将sql server数据库从一台服务器复制到另一台服务器

java - 由于连接超时,无法通过 ImageIO.read(url) 获取图像

c# - Enum.TryParse 的非常基本的使用不起作用

c# - 如何在学校的 Linux 服务器上运行 C# 程序?

SQL Server 查询返回太多记录

sql - 计算不同记录的窗口函数

amazon-web-services - 如何修复 AWS DMS 到 RDS 连接错误?

java - 如何检查代理是否在 Java 中工作?