| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 696 人关注过本帖
标题:连接数据库出现错误。求大神指导!
只看楼主 加入收藏
王祖品
Rank: 2
等 级:论坛游民
帖 子:31
专家分:20
注 册:2013-4-21
结帖率:27.27%
收藏
已结贴  问题点数:10 回复次数:5 
连接数据库出现错误。求大神指导!
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 的连接)}
为什么会出现这种情况?求各位大神指导!!!
搜索更多相关主题的帖子: namespace 指导 连接数据库 private public 
2013-04-25 18:27
shangsharon
Rank: 9Rank: 9Rank: 9
来 自:湖北武汉
等 级:蜘蛛侠
威 望:7
帖 子:221
专家分:1261
注 册:2012-3-25
收藏
得分:0 
SqlConnection conn = new SqlConnection("server=.;Initial Catalog=ClassOnline;User ID=sa;Password=sa");
可能是这里错了,
你的数据库是express版的么,
试试这个
SqlConnection conn = new SqlConnection("server=.\sqlexpress;Initial Catalog=ClassOnline;User ID=sa;Password=sa");
2013-04-25 19:44
王祖品
Rank: 2
等 级:论坛游民
帖 子:31
专家分:20
注 册:2013-4-21
收藏
得分:0 
我改后,出现了错误:无法识别的转义序列。
2013-04-25 22:36
lxb932979339
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:84
专家分:313
注 册:2013-4-24
收藏
得分:5 
\这个是转义字符,写成SqlConnection conn = new SqlConnection("server=.\\sqlexpress;Initial Catalog=ClassOnline;User ID=sa;Password=sa");
或者SqlConnection conn = new SqlConnection(@"server=.\sqlexpress;Initial Catalog=ClassOnline;User ID=sa;Password=sa"); 你再试试
2013-04-26 06:04
王祖品
Rank: 2
等 级:论坛游民
帖 子:31
专家分:20
注 册:2013-4-21
收藏
得分:0 
嗯,这样就可以了。谢谢了
对了,还有一处错误: SqlDataReader sdr = cmd.ExecuteReader();关键字 'User' 附近有语法错误。还得麻烦一下各位大神们!!请多多指导

[ 本帖最后由 王祖品 于 2013-4-26 17:50 编辑 ]
2013-04-26 17:46
lxb932979339
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:84
专家分:313
注 册:2013-4-24
收藏
得分:5 
sql接字符串要有'',你没用'';你用下边的试试再
SqlCommand cmd = new SqlCommand("select * from User where UserName='" + txtName.Text + "'and UserPwd='" + txtPassword.Text + "'", conn);
2013-04-26 17:58
快速回复:连接数据库出现错误。求大神指导!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021835 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved