在使用SQL Server2005的C#中使用ado.net进行尝试和捕获 [英] use of try and catch with ado.net in c# using sql server2005

查看:50
本文介绍了在使用SQL Server2005的C#中使用ado.net进行尝试和捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有登录表,该表中的字段为
用户名(pk)
密码(带有检查约束-如果密码长度小于8,则不接受
电子邮件(唯一)
utype.i编写了一个存储过程,用于在单击提交"按钮时将值插入到登录表中.如果用户名在注册表单中两次输入,email,password,我想使用try and catch捕获异常.运行良好,如果我们按照规则输入,请给我发代码

i am having login table,fields in that table are
username(pk)
password(with check constraint--if length of password is less than 8,it wont accept
email(unique)
utype.i wrote a stored procedure for inserting values into that login table on click of submit button .i want to use try and catch for catching the exceptions if the username if it is entered twice in the registration form,email,password.every thing is working well ,if we enter according to the rules ,plz send me the code

推荐答案

看看创建和设计自定义异常-
http://blog.gurock.com/articles/creating-custom-exceptions-in-dotnet / [^ ]
http://msdn.microsoft.com/en-us/library/ms229064.aspx [ ^ ]
Have a look at creating and designing custom exceptions -
http://blog.gurock.com/articles/creating-custom-exceptions-in-dotnet/[^]
http://msdn.microsoft.com/en-us/library/ms229064.aspx[^]


我刚刚进行了测试,下面的代码有效..您可能需要修改一下才能满足您的要求.

***登录表中的用户名列标记为主键

I just did a test and the following code works..you may have to modify a bit to cater your requirement.

*** the username column in the login table is marked as primary key

Create PROCEDURE spLogin
    @Username varchar
AS
BEGIN
    SET NOCOUNT ON;

    insert into login values (@Username)

END







private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection oConn = new SqlConnection();
            oConn.ConnectionString = "Data source=SOLAP;database=sample;user=sa;password=admin1990";
            oConn.Open();
            string strSQL = "spLogin ''Bala''";
            SqlCommand oCmd = new SqlCommand(strSQL, oConn);
            oCmd.ExecuteNonQuery();
            MessageBox.Show("OK");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);

        }
    }


这篇关于在使用SQL Server2005的C#中使用ado.net进行尝试和捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆