日期:2014-05-20  浏览次数:20858 次

哪错误,提示连不上数据库
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.SqlClient;
namespace 登陆练习
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  private void btnOK_Click(object sender, EventArgs e)
  {
  SqlConnection conn = new SqlConnection();
  conn.ConnectionString = @"Data source =\.;Initial Catalog=lianxi;User Id=sa;PassWord=fengjianhua";
  conn.Open();
  SqlCommand cmd = new SqlCommand();
   
  cmd.Connection = conn;
   
  cmd.CommandText = "select * from T_denglu where UserName=@UserName";
  cmd.Parameters.Add(new SqlParameter("UserName",txtUserName.Text));
  SqlDataReader reader = cmd.ExecuteReader();
  if (reader.Read())
  {
  int errortime = reader.GetInt32(reader.GetOrdinal("ErrorTime"));
  if (errortime > 3)
  {
  MessageBox.Show("登陆次数过多,禁止登陆");
  return;
  }

  string password = reader.GetString(reader.GetOrdinal("PassWord"));
  if (password == txtPassWord.Text)
  {
  MessageBox.Show("登陆成功");
  }
  else
  {
  SqlCommand upcmd = new SqlCommand();
  upcmd.Connection = conn;
  upcmd.CommandText = "update T_denglu ErrorTime=ErrorTime+1 where UserName=@UserName ";
  upcmd.Parameters.Add(new SqlParameter ("UserName",txtUserName.Text));
  upcmd.ExecuteNonQuery();
  MessageBox.Show("密码错误,登陆失败");
  }
  }
  else
  {
  MessageBox.Show("用户名不存在");
  }
  conn.Close();
  conn.Dispose();

  }
   
   
  }
}



在open处提示: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 25 - 连接字符串无效)

------解决方案--------------------
conn.ConnectionString = @"Data source =.;Initial Catalog=lianxi;User Id=sa;PassWord=fengjianhua";

------解决方案--------------------
我也觉得\.有点问题,用.试一试。
------解决方案--------------------
lz已经发过一个贴了,别人给你回复,也没见你在那帖子说行或不行!

conn.ConnectionString =@"Data source=(local);Initial Catalog=lianxi;User ID=sa;Password=fengjianhua";

你加上try{}catch{}语句,断点逐句调试,问题很快就会定位出来。。。
------解决方案--------------------
把连接字符串改了试一下
conn.ConnectionString ="database=lianxi;server=.;uid=sa;pwd=fengjianhua";
实在不行就用windows验证登陆数据库,服务器名称填"(local)"

conn.ConnectionString ="database=lianxi;server=.;integrated security=true";

------解决方案--------------------