用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();
}
}
}
}
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();
}
}
}
}
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("关闭数据库连接成功");
}
}
}
}
这一个测试的时候可以。
string connString = "Data Source=.;Initial Catalog=Master;user id=sa;pwd=2009";
// Initial Catalog=Master 改为你自己定义的数据库
C#访问SQL Server以及数据操作
http://blog.csdn.net/itneste/archive/2009/04/26/4126677.aspx