您的位置:首页 > 数据库

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

2008-02-29 15:25 429 查看
//存储过程

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);

        }

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息