连接数据库出现错误。求大神指导!
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace aa
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
{
MessageBox.Show("请输入用户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (txtPassword.Text == "")
{
MessageBox.Show("请输入密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
SqlConnection conn = new SqlConnection("server=.;Initial Catalog=ClassOnline;User ID=sa;Password=sa");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from User where UserName=" + txtName.Text + "and UserPwd=" + txtPassword.Text + "", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (sdr.HasRows)
{
Form3 frm3 = new Form3();
frm3.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误");
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
this.Hide();
}
private void button3_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtPassword.Text = "";
}
}
}
上面说 conn.Open();!未处理SqlException
{在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接)}
为什么会出现这种情况?求各位大神指导!!!