c#存储过程,返回记录集和执行sql语句

//存储过程

CREATE PROCEDURE  login1 
@userid char(50),
@passwd char(50),
@out char(1) OUTPUT

 AS
begin
select @out=usertype from login where username=@userid and userpass=@passwd
end
GO

//**************************************************************************************

SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=crs;Persist Security Info=false;integrated security=sspi");

        public string  execsqlpro(string u,string p) //执行存储过程。
        {
            if (con.State == ConnectionState.Open)
                con.Close();
           
                con.Open();
                SqlCommand mycmd = new SqlCommand("login1", con);
                mycmd.CommandType = CommandType.StoredProcedure;
                mycmd.Parameters .Add ("@userid",SqlDbType.Char, 50);
                mycmd.Parameters .Add ("@passwd",SqlDbType.Char  ,50);
                mycmd .Parameters ["@userid"].Value =u;
                mycmd.Parameters ["@passwd"].Value =p;
                mycmd.Parameters.Add ("@out",SqlDbType.Char ,1);
                mycmd.Parameters ["@out"].Direction =ParameterDirection .Output ;

                mycmd.ExecuteNonQuery();
                //System.Windows.Forms.MessageBox.Show(mycmd.Parameters["@out"].Value.ToString() );
                return mycmd.Parameters["@out"].Value.ToString();

        }

        public System.Data.DataSet getds(string sqlstr)//此函数返回一个记录集
        {
            System.Data.DataSet sl = new System.Data.DataSet();
            try
            {if (con.State == System.Data.ConnectionState.Open)
                    con.Close();
                else
                {
                    con.Open();
                    //System.Windows.Forms.MessageBox.Show("OK");
                    System.Data.SqlClient.SqlDataAdapter sda = new  System.Data.SqlClient.SqlDataAdapter(sqlstr, con);

                    sda.Fill(sl);

                }
                return sl;

            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("error");
                throw;
            }


        }
        public int execsql(string sqlstr)//此函数直接执行sql语句
        {
            try
            {if (con.State == System.Data.ConnectionState.Open)
                {
                    con.Close();
                    return 0;
                }
                else
                {
                    con.Open();
                    //System.Windows.Forms.MessageBox.Show("OK");
                    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlstr, con);
                    return cmd.ExecuteNonQuery();

                }
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("error");
                throw;
            }
        }


//****************************************************************
 public Boolean loginsuccess=false ;//判断登录是否成功,成功为true
        private void button1_Click(object sender, EventArgs e)
        {string usertpye="";//定义登录用户所属类型
            db d = new db();
            string u=Convert.ToString ( textBox1.Text);
            string p=Convert.ToString ( textBox1.Text);
           string usertype= d.execsqlpro(u, p);//调用存储过程,返回用户登录类型,默认类型为a类,值为a
           if (usertype != "")//验证登录用户,如果返回的用户类型为空,则表示没有此用户。
           {
               //显示form1,关闭登录窗体,注意在porgress.cs文件中添加 Application.Run(new Form1() );
               Form1.ActiveForm.Show();
               loginsuccess = true;
               this.Close();
           }
           else
               MessageBox.Show("用户名密码错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);


        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值