如何在createID表单上检查数据库buttonclick [英] How to check Database on createID form buttonclick

查看:84
本文介绍了如何在createID表单上检查数据库buttonclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Studio 2008和数据库SQL 2005中创建Windows应用程序.我正在创建登录ID和密码表单,但是在我要创建ID表单之前,当我要单击CreateID按钮...时,它必须首先在SQL数据库登录表中检查以前的登录ID ...如果存在登录ID,则必须给出错误例如
"登录ID已经存在"....如何编写代码...

如果有人知道,请帮助我.

I am creating windows application in Visual Studio 2008 and database SQL 2005. i was creating login ID and password form but before i want create ID form in that when i am going to click on CreateID button ....it has to first check for previous login ID in SQL database Login table...if Login ID is present then it have to give error for example
" Login ID is already present " ....how to write the code for it...

please help me if anyone knows...

推荐答案

尝试一下:
Try this:
IF EXISTS(SELECT LoginID FROM Users WHERE LoginID = @LoginID)
BEGIN
    RAISERROR('LoginID already exists.', 1,1)
END
ELSE
BEGIN
    INSERT INTO Users(..........)
    VALUES (............)
END


让数据库来做.如果将loginID设置为唯一列,则尝试添加已经存在的ID会在存储过程中生成错误,该错误会作为异常反映到客户端应用程序中(或者也许从存储proc返回的值将表明一个问题).
Let the database do it. If you set the loginID to be a unique column, trying to add an id that already exists will generate an error in the stored procedure which will be reflected to the client app as an exception (or maybe the return value from the stored proc will indicate a problem).


如果您的登录ID是一个文本框,则在按钮的OnClick事件中:

if your Login ID is a a textbox then in the OnClick event of the button:

string MyCheckQuery = string.Format("SELECT count(0) FROM tblLogin WHERE loginName ='{0}'", textBox1.Text);



该查询可以与SqlCommand一起使用
http://msdn.microsoft.com/en-us/library/877h0y3a.aspx [ ^ ]

如果查询的结果= 0,则名称是唯一的,否则查询的结果为1



That query can be used with an SqlCommand
http://msdn.microsoft.com/en-us/library/877h0y3a.aspx[^]

If the result of the query = 0 then the name is unique, otherwies the result of the query is 1


这篇关于如何在createID表单上检查数据库buttonclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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