大家帮忙看看
我刚学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