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);
}
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);
}
相关文章推荐
- AEF 中的SQL语句执行能否返回数据集(DataTable)?
- 执行SQL语句,返回新插入的主键值
- @@ERROR @@ROWCOUNT 返回的都是上一条SQL 语句后的执行信息。
- hibernate执行的SQL语句返回的list集合用Object[]接收遍历
- 获取执行SQL语句的返回结果
- Java中直接执行sql语句,返回List集合
- 用sp_executesql 来执行拼接的sql语句,并返回值
- Hibernate 执行SQL语句的count函数返回BigDecimal类型
- MSSQL中,执行用字符串拼接的sql语句后返回值
- PHP PDO->exec() 执行无返回结果集的 SQL 语句
- 表型记录集不能执行SQL语句
- Hibernate SQLQuery查询返回空List,在mysql命令下直接执行sql语句可以正常得到记录
- @@ERROR @@ROWCOUNT 返回的都是上一条SQL 语句后的执行信息。
- 获取记录集、执行SQL语句、记录读取
- 使用C#对MySQL数据库执行存在中文的SQL语句,不返回值的问题
- entity framework(EF) 直接执行数据库命令并返回DataTable 参数化sql语句
- PDOstament对象执行execute()函数,只要是sql语句正确都是返回true
- PL_SQL中执行SELECT … INTO …语句时,如果返回结果集为空
- 动态执行sql语句并返回值
- sp_executesql得到执行sql语句的返回值