C#连接SQL server2008 出现错误
Anetwork-relatedorinstance-specificerroroccurredwhileestablishingaconnectiontoSQLServ...
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)
using System;
using System.Data.SqlClient;
public class a
{
public static void Main(string[] args)
{
string source = "server = (loacal);" +
"integrated security = SSPI;" +
"database = nihao";
string select = "update buhao" +
"set age = 10" +
"where name= 'zhang'";
SqlConnection conn = new SqlConnection(source);
conn.Open();
SqlCommand cmd = new SqlCommand(select, conn);
int rowsReturned = cmd.ExecuteNonQuery();
Console.WriteLine("{0} rows affect!");
conn.Close();
Console.WriteLine();
}
}
这是我编写的代码,刚刚才接触C#有关数据的操作 展开
using System;
using System.Data.SqlClient;
public class a
{
public static void Main(string[] args)
{
string source = "server = (loacal);" +
"integrated security = SSPI;" +
"database = nihao";
string select = "update buhao" +
"set age = 10" +
"where name= 'zhang'";
SqlConnection conn = new SqlConnection(source);
conn.Open();
SqlCommand cmd = new SqlCommand(select, conn);
int rowsReturned = cmd.ExecuteNonQuery();
Console.WriteLine("{0} rows affect!");
conn.Close();
Console.WriteLine();
}
}
这是我编写的代码,刚刚才接触C#有关数据的操作 展开
6个回答
展开全部
上面的代码有两处明显错误:
1. 不是(loacal),应该是(local),也可以简写为.
2. update语句buhao和set中应该有空格
请修改代码如下尝试:
using System;
using System.Data;
using System.Data.SqlClient;
public class a
{
public static void Main(string[] args)
{
string source = "server = .;integrated security = SSPI;database = nihao";
string select = "update buhao set age = 10 where name= 'zhang'";
SqlConnection conn = new SqlConnection(source);
SqlCommand cmd = new SqlCommand(select, conn);
conn.Open();
int rowsReturned = cmd.ExecuteNonQuery();
conn.Close();
Console.WriteLine("{0} rows affect!");
Console.ReadLine();
}
}
1. 不是(loacal),应该是(local),也可以简写为.
2. update语句buhao和set中应该有空格
请修改代码如下尝试:
using System;
using System.Data;
using System.Data.SqlClient;
public class a
{
public static void Main(string[] args)
{
string source = "server = .;integrated security = SSPI;database = nihao";
string select = "update buhao set age = 10 where name= 'zhang'";
SqlConnection conn = new SqlConnection(source);
SqlCommand cmd = new SqlCommand(select, conn);
conn.Open();
int rowsReturned = cmd.ExecuteNonQuery();
conn.Close();
Console.WriteLine("{0} rows affect!");
Console.ReadLine();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上面的代码有两处明显错误:
1. 不是(loacal),应该是(local),也可以简写为.
2. update语句buhao和set中应该有空格
请修改代码如下尝试:
using System;
using System.Data;
using System.Data.SqlClient;
public class a
{
public static void Main(string[] args)
{
string source = "server = .;integrated security = SSPI;database = nihao";
string select = "update buhao set age = 10 where name= 'zhang'";
SqlConnection conn = new SqlConnection(source);
SqlCommand cmd = new SqlCommand(select, conn);
conn.Open();
int rowsReturned = cmd.ExecuteNonQuery();
conn.Close();
Console.WriteLine("{0} rows affect!");
Console.ReadLine();
}
}
1. 不是(loacal),应该是(local),也可以简写为.
2. update语句buhao和set中应该有空格
请修改代码如下尝试:
using System;
using System.Data;
using System.Data.SqlClient;
public class a
{
public static void Main(string[] args)
{
string source = "server = .;integrated security = SSPI;database = nihao";
string select = "update buhao set age = 10 where name= 'zhang'";
SqlConnection conn = new SqlConnection(source);
SqlCommand cmd = new SqlCommand(select, conn);
conn.Open();
int rowsReturned = cmd.ExecuteNonQuery();
conn.Close();
Console.WriteLine("{0} rows affect!");
Console.ReadLine();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼主,你要把你写的连接代码贴出来啊,不然怎么知道为什么是错的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请确认你的数据库名称是否正确,否则你的数据库是否支持远程连接。可能是数据库设置的问题。 自己百度下 数据库远程连接的设置。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询