当前位置:编程学习 > C#/ASP.NET >>

C#连接SQL server 2008 的问题

编程环境是Visual Studio 2008 sp1和SQL Server 2008 R2.
部分代码:
string SqlStr = "Server=(local);User Id=sa;Pwd=yyg123;DataBase=student";
SqlConnection con=new SqlConnection(SqlStr);
con.Open();
老是出现如图所示的错误。求解答……

http://zhidao.baidu.com/question/328783782.html?quesup1#replyask-27155686

由于问题带图,这里没法直接发,请大家点击上边的链接查看原问题。不胜感激! --------------------编程问答-------------------- 你用window系统登录试试
stringstr="Server=(local);DataBase=student;Trusted_Connection=True"; --------------------编程问答-------------------- string SqlStr = "Server=.;User Id=sa;Pwd=yyg123;DataBase=student";
或者
string SqlStr = "Server=.;DataBase=student;integrated security=ture";
都试试看 --------------------编程问答--------------------
引用 1 楼 xingchen1008 的回复:
你用window系统登录试试
stringstr="Server=(local);DataBase=student;Trusted_Connection=True";


试过了,不行……相同的问题,也是登录不了…… --------------------编程问答--------------------
引用 2 楼 qzp000000 的回复:
string SqlStr = "Server=.;User Id=sa;Pwd=yyg123;DataBase=student";
或者
string SqlStr = "Server=.;DataBase=student;integrated security=ture";
都试试看


第一种不行,还是登录不了;
第二种提示integrated security的值无效…… --------------------编程问答-------------------- 你数据库服务器开了没.... --------------------编程问答--------------------
引用 5 楼 xingchen1008 的回复:
你数据库服务器开了没....

是啊,你看看打的开sql2008不? --------------------编程问答--------------------
引用 5 楼 xingchen1008 的回复:
你数据库服务器开了没....

呃,什么意思?如果是说在执行程序时把SQL Server 2008得打开并连接的话,那就是开了;如果不是这样,怎么开?谢谢! --------------------编程问答--------------------
引用 6 楼 qzp000000 的回复:
引用 5 楼 xingchen1008 的回复:

你数据库服务器开了没....

是啊,你看看打的开sql2008不?

嗯,直接用SQL Server 2008是可以登录的……这样是打开数据库服务器吧?
谢谢~ --------------------编程问答-------------------- 你先看看能不能打开数据库查看里面的表....或者右击那个我的电脑->管理->服务里面看看SQL服务器打开了没。。。 --------------------编程问答--------------------
引用 3 楼 rui666 的回复:
引用 1 楼 xingchen1008 的回复:
你用window系统登录试试
stringstr="Server=(local);DataBase=student;Trusted_Connection=True";


试过了,不行……相同的问题,也是登录不了……

你把local换成你的计算机名试试.... --------------------编程问答--------------------
引用 9 楼 xingchen1008 的回复:
你先看看能不能打开数据库查看里面的表....或者右击那个我的电脑->管理->服务里面看看SQL服务器打开了没。。。

在我的电脑里,只看到有一项,“SQL Server代理(MSSQLSERVER)”的状态是“已停止”,其它都是正在运行。
谢谢啦~ --------------------编程问答--------------------
引用 10 楼 xingchen1008 的回复:
引用 3 楼 rui666 的回复:
引用 1 楼 xingchen1008 的回复:
你用window系统登录试试
stringstr="Server=(local);DataBase=student;Trusted_Connection=True";


试过了,不行……相同的问题,也是登录不了……

你把local换成你的计算机名试试....


试过了,还是登录不了……
麻烦你了~ --------------------编程问答-------------------- 打开Microsoft SQL Server Management Studio
看第1个根结点,电脑名\实例名 (SQL Server 10.50.1600)
Server=电脑名\实例名 或 Server=(local)\实例名 --------------------编程问答--------------------
引用 13 楼 51crack 的回复:
打开Microsoft SQL Server Management Studio
看第1个根结点,电脑名\实例名 (SQL Server 10.50.1600)
Server=电脑名\实例名 或 Server=(local)\实例名

有错,编译通不过……
3Q! --------------------编程问答-------------------- 晕,编译通不过说明你语法有问题,跟连接字符串有什么关系

string SqlStr = "Data Source=(local)\实例名;Initial Catalog=student;User Id=" sa;Password=yyg123;Connect Timeout=30;"
--------------------编程问答-------------------- string SqlStr = "Data Source=(local)\实例名;Initial Catalog=student;User Id=sa;Password=yyg123;Connect Timeout=30;"; --------------------编程问答-------------------- 一般使用sa都是Server=. --------------------编程问答--------------------
引用 15 楼 51crack 的回复:
晕,编译通不过说明你语法有问题,跟连接字符串有什么关系

string SqlStr = "Data Source=(local)\实例名;Initial Catalog=student;User Id=" sa;Password=yyg123;Connect Timeout=30;"

这样么?还是语法错误……
string SqlStr = "Data Source=(local)\SQLEXPRESS;Initial Catalog=Person;User Id=sa;Password=yyg123;Connect Timeout=30"; --------------------编程问答-------------------- 写个小程序测试下字符串先,字符串没问题就找程序问题 --------------------编程问答-------------------- 为什么感觉你的连接字符串那么别扭啊 ?
string strSql="Data Source=.;Initial Catalog=student;User ID=sa;pwd=sa"
SqlConnection con=new SqlConnection(SqlStr);
con.open();

你用这个看看.... --------------------编程问答--------------------

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;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,
                Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
            reportPath += @"\db_schoolcomputer.mdb";
            string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=" + reportPath;
            OleDbConnection con = new OleDbConnection(ConStr);
            con.Open();
            if (con.State == ConnectionState.Open)
            {
                MessageBox.Show("Access database connected sucessfully", "Access connection");
            }
            else
            {
                MessageBox.Show("Access database connected unsucessfully", "Access connection");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strSql = "Data Source=.;Initial Catalog=Person;User ID=sa;pwd=yyg123";
            SqlConnection con=new SqlConnection(SqlStr);
            con.Open();
            //string SqlStr = "server=.;uid=sa;pwd=yyg123;database=Person";
            //SqlConnection con=new SqlConnection(SqlStr);
            //con.Open();
            if(con.State==ConnectionState.Open)
            {
                MessageBox.Show("success","SQL");
            }
            else
            {
                MessageBox.Show("unsuccess","SQL");
            }
        }
    }
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,