我的代碼工作正常,它說該行已保存,但是當我查看數據庫時沒有保存行。你能幫我嗎?在ASP.NET中使用存儲過程保存數據庫中的行時遇到問題
我想使用存儲過程保存一行,checkifusernameexist
正在工作,但保存功能不保存數據。
這是我的代碼,希望你能理解它。
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.ComponentModel;
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.Collections.Generic;
using System.IO;
namespace storedprocedures
{
public partial class Login : System.Web.UI.Page
{
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["practiceDatabaseConnectionString"].ConnectionString;
}
private void executeAdd(string username, string password)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
conn.Open();
SqlCommand cmd = new SqlCommand("usp_CreateUser", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("userName",userName.Text);
SqlParameter p2 = new SqlParameter("password",password.ToString());
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
SqlDataReader dReader = cmd.ExecuteReader();
if (dReader.HasRows)
{
dReader.Read();
Label3.Text="Record Added!";
}
else
{
Label3.Text="User Error";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(GetConnectionString());
con.Open();
SqlCommand cmd = new SqlCommand("usp_CheckIfUserNameExist",con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("userName", userName.Text);
cmd.Parameters.Add(p1);
SqlDataReader dReader = cmd.ExecuteReader();
if (dReader.HasRows)
{
dReader.Read();
this.Label3.ForeColor = System.Drawing.Color.Red;
this.Label3.Text = "Username already exist!";
}
else
{
this.Label3.Text = "congrats! nanalo ka na!";
this.userName.Text = "";
this.password.Text = "";
}
}
}
}
您需要**顯示我們**您的連接字符串! – 2012-02-29 06:17:14