您的位置:程序门 -> .net技术 -> c#



登录数据库验证用户,第一次写csharp,头都有点大


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


登录数据库验证用户,第一次写csharp,头都有点大
发表于:2007-03-29 17:14:23 楼主
//数据库连接函数
                  public   sqlconnection   dbconnect(string   server,   string   db,   string   uid,   string   pwd)  
                {
                        if   (server   ==   null)//传默认参数不知道格式怎么写
                                server   =   "(local) ";
                        if   (db   ==   null)
                                db   =   "db_cfgl ";
                        if   (uid   ==   null)
                                uid   =   "sa ";
                        if   (pwd   ==   null)
                                pwd   =   "houyichong ";
                        string   strconn= "server= "+server+ ";database= "+db+ ";integrated   security=false;user   id= "+uid+ ";password=houyichong ";
                        sqlconnection   conn   =   new   sqlconnection(strconn);
                        //conn.connectionstring   =strconn;
                        conn.connectiontimeout   =   60;
                        conn.open();
                        return   conn;
                }

//登录按钮的click事件中调用dbconnect

                private   void   btn_login_click(object   sender,   eventargs   e)
                {
                        bool   check;
                        check   =   checkinput(txt_uid.text.tostring().trim())   &&   checkinput(txt_pwd.text.tostring());//checkinput检查textbox输入是否为空
                        if(check==ture)
                        {
                              string   uid=txt_uid.text.trim().tostring();
                              string   pwd=txt_pwd.text.tostring();
                              sqlconnection   conn   =   new   sqlconnection()   ;
                              conn=dbconnect();//调用函数连接数据库
                              sqlcommand     cmd=   conn.createcommand()   ;
                              cmd.commandtext   =   "select   1   from   t_user   where   uid= ' "   +   uid   +   " 'and   pwd= ' "   +   pwd   +   " ' ";
                              cmd.commandtype   =   commandtype.text;
                              sqldataadapter   da   =   new   sqldataadapter(cmd);
                              dataset   ds   =   new   dataset();
                              da.fill(ds);
                              if   (ds.tables[ "t_user "].rows.count   >   0)
                              {
                                      this.visible   =   false;
                                      frmmain.show();
                                      cmd.dispose();
                                      conn.close();
                                      conn.dispose();
                                      ds.dispose();
                                      da.dispose();
                                      frm_login.close();
                              }
                              else
                              {
                                      ilogin++;
                                      msgbox( "无效用户名或密码 ",   "登录失败 ",   messageboxbuttons.ok,   messageboxicon.exclamation);//调用函数msgbox
                              }

                        }
                        else
                        {
                                msgbox( "请输入用户名和密码 ",   "输入数据 ",   messageboxbuttons.ok,   messageboxicon.exclamation);//调用函数msgbox
                        }
                }

//错误:报错的行数正是dbconnect()函数定义的地方
错误 1 类型“system.data.sqlclient.sqlconnection”同时存在于“c:\windows\microsoft.net\framework\v2.0.50727\system.data.dll”和“c:\program   files\microsoft   visual   studio   8\common7\ide\publicassemblies\system.data.sqlclient.dll”中 d:\c#practices\hellocsharp\hellocsharp\form1.cs 78 17 hellocsharp


发表于:2007-03-29 17:20:021楼 得分:0
把命名空间带上看看
system.data.sqlclient.sqlconnection     conn   =   new   system.data.sqlclient.sqlconnection   (strconn);

   
*****************************************************************************
欢迎使用csdn论坛专用阅读器   :   csdn   reader(附全部源代码)  

最新版本:20070212

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
发表于:2007-03-29 17:20:182楼 得分:0
js
发表于:2007-03-29 17:22:143楼 得分:0
你可以用vs2005的   tools-〉connect   to   database
连接一下试试。右边的property窗口有一个connection   string   属性,你对照看看就可以了。
我一般也不记得怎么写connstr,都是这么找的,
发表于:2007-03-29 17:27:504楼 得分:0
//全给你们看

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   hellocsharp
{
        public   partial   class   frm_login   :   form
        {
                private   int   ilogin;
                public   frm_login()
                {
                        initializecomponent();
                }

                private   void   btn_close_click(object   sender,   eventargs   e)
                {
                        this.close();
                }

                private   void   frm_login_load(object   sender,   eventargs   e)
                {

                }

                private   void   btn_login_click(object   sender,   eventargs   e)
                {
                        bool   check;
                        check   =   checkinput(txt_uid.text.tostring().trim())   &&   checkinput(txt_pwd.text.tostring());
                        if(check==ture)
                        {
                              string   uid=txt_uid.text.trim().tostring();
                              string   pwd=txt_pwd.text.tostring();
                              sqlconnection   conn   =   new   sqlconnection()   ;
                              conn=dbconnect();//调用函数连接数据库
                              sqlcommand     cmd=   conn.createcommand()   ;
                              cmd.commandtext   =   "select   1   from   t_user   where   uid= ' "   +   uid   +   " 'and   pwd= ' "   +   pwd   +   " ' ";
                              cmd.commandtype   =   commandtype.text;
                              sqldataadapter   da   =   new   sqldataadapter(cmd);
                              dataset   ds   =   new   dataset();
                              da.fill(ds);
                              if   (ds.tables[ "t_user "].rows.count   >   0)
                              {
                                      this.visible   =   false;
                                      frmmain.show();
                                      cmd.dispose();
                                      conn.close();
                                      conn.dispose();
                                      ds.dispose();
                                      da.dispose();
                                      frm_login.close();
                              }
                              else
                              {
                                      ilogin++;
                                      msgbox( "无效用户名或密码 ",   "登录失败 ",   messageboxbuttons.ok,   messageboxicon.exclamation);//调用函数msgbox
                              }

                        }
                        else
                        {
                                msgbox( "请输入用户名和密码 ",   "输入数据 ",   messageboxbuttons.ok,   messageboxicon.exclamation);//调用函数msgbox
                        }
                }
                //检查文本框是否为空的函数
                private   bool   checkinput(string   s)  
                {
                        bool   bflag;
                        if(s.length   > 0)  
                                return   true;
                        else
                                return   false;        
                }
                //数据库连接函数
                  public   sqlconnection   dbconnect(string   server,   string   db,   string   uid,   string   pwd)  
                {
                        if   (server   ==   null)
                                server   =   "(local) ";
                        if   (db   ==   null)
                                db   =   "db_cfgl ";
                        if   (uid   ==   null)
                                uid   =   "sa ";
                        if   (pwd   ==   null)
                                pwd   =   "houyichong ";
                        string   strconn= "server= "+server+ ";database= "+db+ ";integrated   security=false;user   id= "+uid+ ";password=houyichong ";
                        sqlconnection   conn   =   new   sqlconnection(strconn);
                        //conn.connectionstring   =strconn;
                        conn.connectiontimeout   =   60;
                        conn.open();
                        return   conn;
                }
                //定义各种返回客户端的消息函数
                private   void   msgbox(string   txt,   string   title,   messageboxbuttons   bnt,messageboxicon   icon)  
                {
                        messagebox.show(txt,   title,   bnt,icon   );
                }
        }
}
发表于:2007-03-29 17:31:555楼 得分:0
把命名空间带上看看
system.data.sqlclient.sqlconnection     conn   =   new   system.data.sqlclient.sqlconnection   (strconn);

sqlcommand,sqlconnection,dataadapter,dataset定义实例的时候都要这么长一大窜吗?
大家帮帮忙呀,第一次写c#,上面的类层次和结构不太懂了,看了两天c#语言就写程序了.


快速检索

最新资讯
热门点击