大家帮忙看看

cq625652580 2008-07-27 05:12:40
我刚学ASP.NET2.0,这是我用存储过程写的一个往数据库中写入数据的过程,可以查重.我感觉有点麻烦,哪位大虾有没有简单的写法.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Procedure : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource sqlDS = new SqlDataSource();
ConnectionStringSettings connSettings = ConfigurationManager.ConnectionStrings["ConnectionString"];
SqlConnection conn = new SqlConnection(connSettings.ConnectionString);
SqlCommand cmd = new SqlCommand("dbo.authorInfo",conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
cmd.Parameters.Add(new SqlParameter("@au_name", SqlDbType.Char, 10, "this.TextBox1.Text"));
cmd.Parameters.Add(new SqlParameter("@city", SqlDbType.VarChar, 50, "this.TextBox2.Text"));
cmd.Parameters.Add(new SqlParameter("@telephone", SqlDbType.Int, 4, "this.TextBox3.Text"));
cmd.Parameters.Add(new SqlParameter("@address", SqlDbType.VarChar, 50, "this.TextBox4.Text"));
cmd.Parameters.Add(new SqlParameter("@au_id", SqlDbType.Int, 0, ParameterDirection.Output, false, 0, 0, "au_id", DataRowVersion.Default, null));
cmd.Parameters.Add(new SqlParameter("@nameCount", SqlDbType.Int, 0, ParameterDirection.Output, false, 0, 0, "nameCount", DataRowVersion.Default, null));
cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
cmd.Parameters["@au_name"].Value = this.TextBox1.Text;
cmd.Parameters["@city"].Value = this.TextBox2.Text;
cmd.Parameters["@telephone"].Value = this.TextBox3.Text;
cmd.Parameters["@address"].Value = this.TextBox4.Text;
cmd.Parameters["@nameCount"].Direction = ParameterDirection.Output;
SqlDataReader dr = cmd.ExecuteReader();
int nameCount = (int)cmd.Parameters["@nameCount"].Value;
if (nameCount == 2)
{
Label1.Text = "此用户名已存在!";
if (this.TextBox1.Text != String.Empty)
{
System.Threading.Thread.Sleep(100);
this.TextBox1.Text = "";
}
}
if (nameCount == 1)
{
Label2.Text = "添加成功!";
}

}

}


存储过程
cteate PROCEDURE dbo.authorInfo
@au_name char(10),
@city varchar(50),
@telephone int,
@address varchar(50),
@au_id int output,
@nameCount int output
AS
DECLARE @Count int
-- 查找是否有相同名称的记录
SELECT @Count = Count(au_id) FROM authors WHERE
au_name = @au_name
IF @Count = 0
begin
INSERT INTO authors(au_name,city,telephone,address) values
(@au_name,@city,@telephone,@address)
set @nameCount=1
end
else
begin
set @nameCount=2
end
...全文
27 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
luofuxian 2008-07-27
[Quote=引用 2 楼 chaye12 的回复:]
呵呵,应该说是正规思路,能简单点的地方就是添加参数的地方可以合并下,例如:
cmd.Parameters.Add(new SqlParameter("@au_name", SqlDbType.Char, 10, "this.TextBox1.Text"));
cmd.Parameters["@au_name"].Value = this.TextBox1.Text; 可以合并成一句,但是你这样写也可以,对新手来说,能不省略语句就别省略,这样基础掌握的就会牢固点!
[/Quote]
回复
chaye12 2008-07-27
呵呵,应该说是正规思路,能简单点的地方就是添加参数的地方可以合并下,例如:
cmd.Parameters.Add(new SqlParameter("@au_name", SqlDbType.Char, 10, "this.TextBox1.Text"));
cmd.Parameters["@au_name"].Value = this.TextBox1.Text; 可以合并成一句,但是你这样写也可以,对新手来说,能不省略语句就别省略,这样基础掌握的就会牢固点!
回复
mylibin 2008-07-27
http://topic.csdn.net/u/20080517/09/4075930e-d58e-4073-83b3-148e9f8f2205.html
回复
相关推荐
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2008-07-27 05:12
社区公告
暂无公告