| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3162 人关注过本帖
标题:C#中代码写的正确,但是F5的时候却说数据库连接失败啊
只看楼主 加入收藏
chj6818112
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-11-25
结帖率:100%
收藏
 问题点数:0 回复次数:36 
C#中代码写的正确,但是F5的时候却说数据库连接失败啊
代码如下:
namespace Link
{
    class DBHelper
    {
      //连接数据库事件
        public static string conString = "Data Source=MICROSOF-237019;Initial Catalog=User;User ID=sa";
        public static SqlConnection openConnection()
        {
            SqlConnection con = null;
            try
            {
                con =new SqlConnection(conString);
                con.Open();
            }
            catch(Exception e)
            {
                MessageBox.Show("连接有误");
            }
            return con;
 
        }
        public static void closeConnection(SqlConnection con)
        {
            try
            {
               if(con!=null)
               {
                   con.Close();
               }
            }
            catch(Exception e)
            {
                MessageBox.Show("关闭数据库有问题");
            }
        }
    }
}

设计窗体后
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Link
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //登陆的Clicl事件
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = this.txtUser.Text;
            string password = this.txtPass.Text;
            string type = this.cmbType.Text;
            switch (type)
            {
                case "学生":
                    UserManager um1 = new UserManager();
                    bool isOk1 = um1.login(username, password);
                    if (isOk1)
                    {
                        MessageBox.Show("登陆成功");

                    }
                    else
                    {
                        MessageBox.Show("登陆失败");
                    }
                    break;
                case "教师":
                    UserManager um2 = new UserManager();
                    bool isOk2 = um2.login(username, password);
                    if (isOk2)
                    {
                        MessageBox.Show("登陆成功");

                    }
                    else
                    {
                        MessageBox.Show("登陆失败");
                    }
                    break;
                default:
                    MessageBox.Show("类型有误");
                    break;
            }
        }
        //退出的事件
        private void bunCanal_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

//运行的程序
namespace Link
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

做的一个简单的登陆系统的
我生成都是成功的哇,没有报错,但是运行结束后就会报错啊,说连接有误啊,是不是我家里的SQL数据库有问题啊,但是我在我朋友家运行好好的啊,请各位高手帮帮忙啊,在这先谢谢啦!
搜索更多相关主题的帖子: 数据库 代码 失败 
2008-11-25 22:21
天使不哭
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:677
专家分:22
注 册:2006-7-9
收藏
得分:0 
把你的UserManager类代码发一下,我看一下你方法写的对不对。
"Data Source=MICROSOF-237019;Initial Catalog=User;User ID=sa";
这句改成"Server=.;uid=sa,pwd=密码"注意密码不要为空,数据库没密码的你自己设一个密码。

C#Winform技术群:25380362
博客:http:///boyliupan/
2008-11-25 22:39
chj6818112
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-11-25
收藏
得分:0 
代码
USerManaager代码

using System;
using System.Collections.Generic;
using System.Text;

namespace Link
{
    class User
    {
        public int id;
        public string loginname;
        public string password;
    }
}




using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Link
{
    class UserManager
    {
        //定义一个布尔变量
        public bool login(string username, string password)
        {
            bool isLoginOk = false;
            SqlConnection con = null;
            try
            {
                //打开数据库连接
                con = DBHelper.openConnection();
                //进行数据库核对
                string sql = "select * from tb_User where loginname='" + username + "' and password='" + password + "'";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    isLoginOk = true;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("登陆查询出错");
            }
            finally
            {
                DBHelper.closeConnection(con);
            }
            return isLoginOk;           
        }

        public bool insert(User user)
        {
            bool isInsertOk = false;
            SqlConnection con = null;
            try
            {
                con = DBHelper.openConnection();
                string sql = "insert into tb_User(loginname,password)values('{0}','{1}')";
                sql=string.Format(sql, user.loginname, user.password);

                SqlCommand cmd = new SqlCommand(sql, con);
                int i=cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    isInsertOk = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("添加出错");
            }
            finally
            {
                DBHelper.closeConnection(con);
            }
            return isInsertOk;
        }
    }
}
2008-11-25 22:45
天使不哭
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:677
专家分:22
注 册:2006-7-9
收藏
得分:0 
调试的时候到哪里出错了?

C#Winform技术群:25380362
博客:http:///boyliupan/
2008-11-25 22:47
chj6818112
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-11-25
收藏
得分:0 
回复 第2楼 天使不哭 的帖子
USerManaager代码

using System;
using System.Collections.Generic;
using System.Text;

namespace Link
{
    class User
    {
        public int id;
        public string loginname;
        public string password;
    }
}




using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Link
{
    class UserManager
    {
        //定义一个布尔变量
        public bool login(string username, string password)
        {
            bool isLoginOk = false;
            SqlConnection con = null;
            try
            {
                //打开数据库连接
                con = DBHelper.openConnection();
                //进行数据库核对
                string sql = "select * from tb_User where loginname='" + username + "' and password='" + password + "'";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    isLoginOk = true;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("登陆查询出错");
            }
            finally
            {
                DBHelper.closeConnection(con);
            }
            return isLoginOk;           
        }

        public bool insert(User user)
        {
            bool isInsertOk = false;
            SqlConnection con = null;
            try
            {
                con = DBHelper.openConnection();
                string sql = "insert into tb_User(loginname,password)values('{0}','{1}')";
                sql=string.Format(sql, user.loginname, user.password);

                SqlCommand cmd = new SqlCommand(sql, con);
                int i=cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    isInsertOk = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("添加出错");
            }
            finally
            {
                DBHelper.closeConnection(con);
            }
            return isInsertOk;
        }
    }
}
2008-11-25 22:48
chj6818112
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-11-25
收藏
得分:0 
回复 第4楼 天使不哭 的帖子
调试后就显示说用户SA登陆失败!!
2008-11-25 22:48
chj6818112
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-11-25
收藏
得分:0 
回复 第4楼 天使不哭 的帖子
就显示用户SA失败啊!
我刚才上网查了下
好像是说我的数据库出错了
但是我在我朋友家也是同样的
他的电脑上可以的啊
到底出在哪里啊!
2008-11-25 22:50
chj6818112
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-11-25
收藏
得分:0 
回复 第4楼 天使不哭 的帖子
我吧我的那个解压给你

你的QQ多少啊
在线不!
2008-11-25 22:52
天使不哭
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:677
专家分:22
注 册:2006-7-9
收藏
得分:0 
你用的什么环境,说下,VS版本多少,SQL 版本多少,我交你个方法查下哪个地方出错了

C#Winform技术群:25380362
博客:http:///boyliupan/
2008-11-25 22:53
天使不哭
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:677
专家分:22
注 册:2006-7-9
收藏
得分:0 
以下是引用chj6818112在2008-11-25 22:52的发言:

我吧我的那个解压给你

你的QQ多少啊
在线不!

不用发给我,我没装SQL

C#Winform技术群:25380362
博客:http:///boyliupan/
2008-11-25 22:54
快速回复:C#中代码写的正确,但是F5的时候却说数据库连接失败啊
数据加载中...
 
   



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

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