在vs2010中连接sqlserver2005的代码怎么写,请高手指点

leiheni 2012-11-16 10:56:49
如题,知道的大侠指教下哦,附上代码,并简单的说明,谢啦
...全文
517 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
leiheni 2012-11-16
  • 打赏
  • 举报
回复
谢谢各位的回复,底层的操作会了,现在想把底层的操作封装成一个新的类,使调用方法是更方便,今天弄了下好像不太好弄,为了得到及时的帮助,我把该贴结了另立题求教,自己也还在探索中。。。
發糞塗牆 2012-11-16
  • 打赏
  • 举报
回复
这格式真恶心
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 BookHouseMag 
 { 
     public partial class Login : Form 
     { 
         public Login() 
         { 
             InitializeComponent(); 
         } 
   
         int i= 2; //密码连续输入三次错误将关闭登录窗口,i用来记录登录次数 
   
         private void btnYes_Click(object sender, EventArgs e)  
         { 
             string userNo = txtNo.Text;     //用户编号 
             string password = txtPwd.Text;  //用户密码 
             string levels = cboLevel.Text; 
             if (userNo == "" || password == "")  //没有输入用户名或者密码给予提示 
             { 
                 MessageBox.Show("请输入完整的用户名和密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
             } 
             else
             { 
                 string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Book;Integrated Security=True"; 
                 SqlConnection connection = new SqlConnection(connString);   //连接到引入的数据库 
   
                 connection.Open();  // 打开数据库连接   
                 string sql = String.Format("select count(*) from [User] where workerno='{0}'and password='{1}' and level= '{2}'", userNo, password, levels);  //获取 
   
 用户名和密码匹配的行的数量的SQL语句    
                 SqlCommand command = new SqlCommand(sql, connection);   //创建 Command 对象 
                 int num = (int)command.ExecuteScalar();  //执行查询语句,返回匹配的行数 
                 if (num > 0)    //如果有匹配的行,则表明用户名、密码和权限正确 
                 {                        
                     MessageBox.Show("欢迎进入图书仓库管理系统!", "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                     this.Hide();  //隐藏登录窗体 
                     MainFrm mainForm = new MainFrm();// 创建主窗体对象  
                     if (levels == "普通管理员")  //如果是普通管理员登录,怎不能使用员工信息的功能 
                     { 
                         mainForm.levels1();  //调用主函数中自己定义的函数(不能使用员工信息的供能) 
                     } 
                     else  //如果是特权管理员 
                     { 
                         mainForm.levels2();  //可以使用员工信息功能,调用自己定义的函数 
                     } 
                     mainForm.transmit(txtNo.Text);  //将员工编号放进主窗体,transmit()是主窗体的函数 
                     mainForm.ShowDialog(); // 显示窗体                    
                     this.Close();  // 显示窗体执行完毕后,登录窗体关闭 
                  } 
                  else  //没有匹配的行,表明输入的用户名、密码或者输入的权限错误不正确 
                  { 
                     if (i == 0) //当i=0时,表明已经三次尝试登录 
                     { 
                         MessageBox.Show("已三次输入错误,登录界面关闭!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
                         this.Close();  //关闭登录窗体 
                     } 
                     else  //输入错误,但是没有到三次 
                     { 
                         MessageBox.Show("您输入的用户名或密码错误或者选择了错误的登录权限,还有" + i + " 次机会!", "登录失败", MessageBoxButtons.OK,  
   
 MessageBoxIcon.Exclamation); 
                         i = i - 1;  //将i的值减1 
                     } 
                 } 
                 connection.Close();// 关闭数据库连接 
             } 
               
         }
發糞塗牆 2012-11-16
  • 打赏
  • 举报
回复
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 BookHouseMag  {      public partial class Login : Form      {          public Login()          {              InitializeComponent();          }             int i= 2; //密码连续输入三次错误将关闭登录窗口,i用来记录登录次数             private void btnYes_Click(object sender, EventArgs e)           {              string userNo = txtNo.Text;     //用户编号              string password = txtPwd.Text;  //用户密码              string levels = cboLevel.Text;              if (userNo == "" || password == "")  //没有输入用户名或者密码给予提示              {                  MessageBox.Show("请输入完整的用户名和密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);              }              else             {                  string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Book;Integrated Security=True";                  SqlConnection connection = new SqlConnection(connString);   //连接到引入的数据库                     connection.Open();  // 打开数据库连接                    string sql = String.Format("select count(*) from [User] where workerno='{0}'and password='{1}' and level= '{2}'", userNo, password, levels);  //获取     用户名和密码匹配的行的数量的SQL语句                     SqlCommand command = new SqlCommand(sql, connection);   //创建 Command 对象                  int num = (int)command.ExecuteScalar();  //执行查询语句,返回匹配的行数                  if (num > 0)    //如果有匹配的行,则表明用户名、密码和权限正确                  {                                             MessageBox.Show("欢迎进入图书仓库管理系统!", "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information);                      this.Hide();  //隐藏登录窗体                      MainFrm mainForm = new MainFrm();// 创建主窗体对象                       if (levels == "普通管理员")  //如果是普通管理员登录,怎不能使用员工信息的功能                      {                          mainForm.levels1();  //调用主函数中自己定义的函数(不能使用员工信息的供能)                      }                      else  //如果是特权管理员                      {                          mainForm.levels2();  //可以使用员工信息功能,调用自己定义的函数                      }                      mainForm.transmit(txtNo.Text);  //将员工编号放进主窗体,transmit()是主窗体的函数                      mainForm.ShowDialog(); // 显示窗体                                         this.Close();  // 显示窗体执行完毕后,登录窗体关闭                   }                   else  //没有匹配的行,表明输入的用户名、密码或者输入的权限错误不正确                   {                      if (i == 0) //当i=0时,表明已经三次尝试登录                      {                          MessageBox.Show("已三次输入错误,登录界面关闭!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                          this.Close();  //关闭登录窗体                      }                      else  //输入错误,但是没有到三次                      {                          MessageBox.Show("您输入的用户名或密码错误或者选择了错误的登录权限,还有" + i + " 次机会!", "登录失败", MessageBoxButtons.OK,      MessageBoxIcon.Exclamation);                          i = i - 1;  //将i的值减1                      }                  }                  connection.Close();// 关闭数据库连接              }                         }
  • 打赏
  • 举报
回复
我奉劝楼主,自己多学习,百度,360搜索,谷歌都可以,这样的小事是应该你自己学习的东西,这样的事情都来问,自己要主动学习.否则你将一事无成.
坚_持 2012-11-16
  • 打赏
  • 举报
回复
//导入命名空间 也就是连接数据库的头文件 using System.Data.SqlClient; //数据库的连接地址 string url = "server = .;database = 数据库名称;user id = sa;pwd = sql2005的密码"; //连接数据库后执行的语句 string select = "select * from 表名 where 条件 "; //连接数据库 SqlConnection scn = new SqlConnection(url); //执行语句函数 SqlCommand scm = scn.CreateCommand(); //得到需要执行的语句 scm.CommandText = select; //打开数据库 scn.Open(); //读取的方式 SqlDataReader sdr = scm.ExecuteReader(); // sdr.HasRows 返回受影响的行 while (sdr.Read()) { ............//读出表里的数据 并输出 }

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧