简体   繁体   English

从 Visual Studio 连接到 SQL Server 数据库的问题

[英]Issue with connecting to SQL Server database from Visual Studio

I've created a simple Windows Forms application in C# and now find it hard to connect it to a SQL Server database.我已经用 C# 创建了一个简单的 Windows 窗体应用程序,现在发现很难将它连接到 SQL Server 数据库。 I've created the database within Visual Studio itself.我已经在 Visual Studio 中创建了数据库。 However, once I try to insert data I get a SQL exception.但是,一旦我尝试插入数据,就会出现 SQL 异常。 My guess is there is problem in the connection data source ie the con variable.我的猜测是连接数据源中存在问题,即 con 变量。 Please help me find a solution.请帮我找到解决办法。

SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=Database1;Integrated Security=SSPI");

SqlCommand cmd;
SqlDataAdapter adapt;

if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
{
    cmd = new SqlCommand("insert into [dbo].[user] (Id, username, password) values (@id, @name, @state)", con);

    con.Open();

    cmd.Parameters.AddWithValue("@id", textBox1.Text);
    cmd.Parameters.AddWithValue("@name", textBox2.Text);
    cmd.Parameters.AddWithValue("@state", textBox3.Text);

    cmd.ExecuteNonQuery();
    con.Close();

    MessageBox.Show("Record Inserted Successfully");
}
else 
{
    MessageBox.Show("Please Provide Details!");
}

The database file name is数据库文件名是

\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf

If you created the database on an actual instance of SQL Server (this can be a local instance on your machine), it appears as though your connection string's data source isn't quite right.如果您在 SQL Server 的实际实例(这可以是您机器上的本地实例)上创建数据库,则连接字符串的数据源似乎不太正确。

Data Source=DESKTOP-XXXXXXX/Shenal Burkey数据源=DESKTOP-XXXXXXX/Shenal Burkey

There are two problems with this, first the slash (/) should actually be a backslash ().这有两个问题,首先斜杠 (/) 实际上应该是一个反斜杠 ()。 Second, after the slash you have 'Shenal Burkey' with a space.其次,在斜线之后你有一个带空格的“Shenal Burkey”。 SQL Server instance names cannot contain space characters. SQL Server 实例名称不能包含空格字符。 That being said, if you installed SQL Server as a named instance, you need to specify the named instance in that place, if you accepted the default instance name it should be 'MSSQLSERVER'.也就是说,如果您将 SQL Server 作为命名实例安装,则需要在该位置指定命名实例,如果您接受默认实例名称,则它应该是“MSSQLSERVER”。 If you are using the default instance name, you can either specify your connection string like this:如果您使用默认实例名称,您可以指定您的连接字符串,如下所示:

Data Source=DESKTOP-XXXXXXX\MSSQLSERVER

or you can omit the instance name entirely and just use:或者您可以完全省略实例名称并使用:

Data Source=DESKTOP-XXXXXXX

Also, just a tip, you will notice I replaced the specifics of your DESKTOP's hostname with X's, personally I'd recommend editing your question and doing the same.另外,只是一个提示,您会注意到我用 X 替换了您桌面主机名的细节,我个人建议编辑您的问题并执行相同操作。 It may not seem like it would be useful, but someone might just pick that up and find a crafty way to do some damage.它可能看起来没有用,但有人可能会捡起它并找到一种狡猾的方法来造成一些伤害。 Better safe than sorry.安全总比后悔好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Visual Studio连接到SQL Server Express - Connecting to SQL Server Express from Visual Studio 在 Visual Studio 2008 中连接到 SQL Server 2012 数据库 - Connecting to a SQL Server 2012 database in Visual Studio 2008 将SQL Server 2012数据库连接到Visual Studio 2012 - Connecting SQL Server 2012 database to Visual Studio 2012 通过Visual Studio 2012连接到SQL Server数据库 - Connecting to SQL Server Database via Visual Studio 2012 从Visual Studio 2013连接到SQL Server 2008 R2数据库 - Connecting to SQL Server 2008 R2 database from Visual Studio 2013 从虚拟机中的Visual Studio 2010调试会话连接到SQL Server数据库 - Connecting to SQL Server database from a Visual Studio 2010 debugging session in a virtual machine 在Visual Studio中使用SQL Server中的数据库 - Using Database from SQL Server in Visual Studio 从Visual Studio 2010 for Windows Phone环境连接到SQL Server - Connecting to SQL Server from Visual Studio 2010 for windows phone environment SQL 数据库未连接到 Visual Studio 2015 - SQL database not connecting to the visual studio 2015 注册表单Visual Studio中的SQL Server数据库问题 - Issue with SQL Server database in Registration form Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM