我似乎无法连接到我的本地数据库。 每次运行它,它都会弹出一个空白窗口(空白命令行窗口)。C#和SQL Server连接
我错过了什么?
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
namespace dbtest1
{
class Program
{
static void Main(string[] args)
{
string myConnectionString = "Initial Catalog=myDB;Data Source=localhost;Integrated Security=SSPI;";
SqlConnection myConnection = new SqlConnection(myConnectionString);
string myInsertQuery = "INSERT INTO tts (min, max, average, lh, stdev, main_id) Values(5,5,5,'ASU',5,55)";
SqlCommand myCommand = new SqlCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
}
}
当我运行调试它给我的错误在myConnection.Open();
SqlException was unhandled: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
你是不是实际上将任何内容输出到控制台窗口,这就是为什么它是空白的。你得到什么实际的错误? – 2011-06-08 22:48:41
当我运行调试它给我在myConnection.Open();错误; SqlException未处理:与SQL Server建立连接时发生网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,并将SQL Server配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接) – 2011-06-09 14:29:55