C#2010 Express和#x2B;SQL Server 2008 Express-连接“;“登录失败”;
实际上,我正在用Visual C#Express 2010开发一个Windows窗体应用程序,它将使用SQL Server 2008 Express数据库中的(读/写)数据 我已使用SQL Server Management Studio(2008 Express)创建了数据库, 我知道该实例名为C#2010 Express和#x2B;SQL Server 2008 Express-连接“;“登录失败”;,c#,sql-server-2008,connection,C#,Sql Server 2008,Connection,实际上,我正在用Visual C#Express 2010开发一个Windows窗体应用程序,它将使用SQL Server 2008 Express数据库中的(读/写)数据 我已使用SQL Server Management Studio(2008 Express)创建了数据库, 我知道该实例名为ATLELAG786576\SQLEXPRESS 我的数据库称为“测试” 查看SQL Server Management Studio(2008 Express)中我的数据库“测试”属性: 在文件下,我
ATLELAG786576\SQLEXPRESS
我的数据库称为“测试”
查看SQL Server Management Studio(2008 Express)中我的数据库“测试”属性:
在文件下,我是数据库的所有者(ATLE\bnevux
)
在安全、登录、Mylogin(ATLE\bNevUX)下查找
- 我的默认数据库是“TEST”
- 服务器角色为“public”+“sysadmin”
- 用户映射数据库'TEST'用户'dbo'默认架构'dbo'
<?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>
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;
}
}
}
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();
}
}
}
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");
}
}
}
dbAccess.cs:
<?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>
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;
}
}
}
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();
}
}
}
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");
}
}
}
格式为employeeAdd.cs:
<?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>
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;
}
}
}
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();
}
}
}
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):无法打开登录请求的数据库“D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Data\TEST.mdf”。登录失败。
用户“ATLE\bNevUX”登录失败
请注意,我从未真正能够在Visual C#2010 Express中添加我的数据源,因此我可以从VS中管理数据库,我总是收到以下错误消息:
无法打开物理文件“D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf”。操作系统错误32:“32(Pout-Pas-AcE.E.C.A.)
尝试为文件D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf附加自动命名数据库失败。存在同名数据库,或无法打开指定的文件,或该文件位于UNC共享上
试着替换
Initial Catalog=D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf
简单地
Initial Catalog=TEST
嗨,jeroenh,刚刚尝试了你的建议,它奏效了,解决了我的问题!谢谢你的快速回复!(我如何将此答案标记为成功?)很高兴它有效。问题的左边应该有一个“接受”标记。您也可以使用向上箭头进行向上投票。jeroenh,我可以滥用并询问您如何解决使用Visual C#2010 Express浏览/管理数据库的可能性;当我进入数据源>新建>数据库>数据集>新建连接时,我选择“数据源”Microsoft SQL Server文件。。。(很抱歉,如果翻译不正确,我使用的是法语版本)然后在单击“测试连接”时浏览到我的TEST.mdf文件,我现在收到以下错误消息“无法打开用户默认数据库。登录失败。用户“ATLE\bNevux”登录失败。谢谢!Briceno不能完全确定VS Express,但您不需要浏览到.mdf文件。您需要输入SQL server名称(COMPUTER\INSTANCE),并连接到数据库名称(“TEST”),VS Express明确要求我指向.mdf文件。我按照您的建议做了+将“初始目录”属性更改为我的Db名称,并且成功了;)谢谢你的帮助。祝你有一个愉快的一天