我的代码工作正常,它表示行已保存,但是当我查看数据库时,没有保存行。你能帮帮我吗?
我想使用存储过程保存一行,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 = "";
}
}
}
}
答案 0 :(得分:3)
使用cmd.ExecuteNonQuery()
添加记录。