用vs2005连接SQl2005数据库时,为什么连接sql原来自带的数据库(如master)可以连接成功,而自己创建的数据库却不能连接,提示sa登陆失败,我的sql已经开了数据库验证和windows验证,已开远程连接,且用sa登陆master成功。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace OpenCloseDB
{
    /// <summary>
    /// 本示例演示使用ExecuteScalar()方法查询学员表中学员信息的数量。 
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        // 打开数据库连接,查询学员记录数量
        private void btnTest_Click(object sender, EventArgs e)
        {
            // 创建 Connection 对象
            string connString = "Data Source=.;Initial Catalog=Master;user id=sa;pwd=2009";
            SqlConnection connection = new SqlConnection(connString);            int num = 0;          // 选员信息的数量
            string message = "";  // 弹出的结果消息
            // 查询用的 SQL 语句
            string sql = "SELECT COUNT(*) FROM Student";            try
            {
                connection.Open();// 打开数据库连接
                // 创建 Command 对象
                SqlCommand command = new SqlCommand(sql, connection);
                // 执行 SQL 查询
                num = (int)command.ExecuteScalar();                message = string.Format("Student表中共有{0}条学员信息!",num);
                MessageBox.Show(message,"查询结果",MessageBoxButtons.OK,MessageBoxIcon.Information);            }
            catch (Exception ex)
            {
                // 操作出错
                MessageBox.Show(ex.Message);
            }
            finally
            {
                // 关闭数据库连接
                connection.Close();                
            }
        }
    }
}

解决方案 »

  1.   

    http://www.connectionstrings.com/
      

  2.   

    谢谢,我的连接语句没有问题啊,对数据库没有操作仅是测试连接的时候是可以建立连接的,只是换成我自己建的数据库之后就没法建立连接了。using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace OpenCloseDB
    {
        /// <summary>
        /// 本示例演示打开和关闭数据库,并使用异常处理。
        /// </summary>
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        // 测试打开数据库的操作,理解finally
            private void btnTest_Click(object sender, EventArgs e)
            {
                string connString = "Data Source=.;Initial Catalog=master;User ID=sa;pwd=2009";
                SqlConnection connection = new SqlConnection(connString);
                try
                {
                    // 打开成功
                    connection.Open();
                    MessageBox.Show("打开数据库连接成功");                
                }
                catch (Exception ex)
                {
                    // 打开失败
                    MessageBox.Show(ex.Message);
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    // 关闭数据库
                    connection.Close();
                    MessageBox.Show("关闭数据库连接成功");
                }
            }
        }
    }
    这一个测试的时候可以。
      

  3.   


     string connString = "Data Source=.;Initial Catalog=Master;user id=sa;pwd=2009";
     // Initial Catalog=Master 改为你自己定义的数据库
      

  4.   

    哎、终于明白了,用windows身份验证登陆的时候默认登陆的是vs2005安装时的SQLExpress版的数据库,我们在程序中如果用sa登陆,data source后面仅仅加一个.的话默认登陆的就是我们自己安装的SQL,我在完整版的SQL里建好了那个数据库之后就可以了,谢谢各位的帮助尤其是qq306850549 的提醒,我说的对吗?如果不对还请各位斧正昂。斧正完成后散分。谢谢。
      

  5.   

    欢迎访问我的博客 程序员日记 http://www.ideaext.com
      

  6.   

    看看:
    C#访问SQL Server以及数据操作
      

  7.   

    ^hehe正好这两天要弄个连接数据库的,平常只是看书没多少时间,借鉴下。哈哈。
      

  8.   

    问题分析及解决方法,已贴,谢谢各位的支持,欢迎斧正。
    http://blog.csdn.net/itneste/archive/2009/04/26/4126677.aspx