我的程序不会在数据库中保存或加载信息
问题描述:
我创建了一个数据库和三个页面用于注册&登录表格&第三个用于我的用户名之间的抽奖。 我没有任何错误,但整个事情没有使用数据库, 它不保存或从数据库加载。我的程序不会在数据库中保存或加载信息
这是我的登记表:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _2 : System.Web.UI.Page
{
protected void btnRegister_Click(object sender, EventArgs e)
{
string strcon = "Data Source=.;uid=sa;pwd=123;database=Login_Register";
SqlConnection con = new SqlConnection(strcon);
SqlCommand com = new SqlCommand("strlogin", con);
com.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("username", txtUserName.Text.ToString());
SqlParameter p2 = new SqlParameter("email", txtEmail.Text.ToString());
SqlParameter p3 = new SqlParameter("password", txtpassword.Text.ToString());
com.Parameters.Add(p1);
com.Parameters.Add(p2);
com.Parameters.Add(p3);
con.Open();
Label4.Text ="ثبت نام با موفقیت انجام شد".ToString();
Label4.Visible = true;
con.Close();
com.ExecuteNonQuery();
}
private SqlDbType ToString(object text)
{
throw new NotImplementedException();
}
}
public class Label4
{
internal static object Text;
internal static bool Visible;
}
public class txtUserName
{
internal static object Text;
internal static bool Visible;
}
public class txtEmail
{
internal static object Text;
internal static bool Visible;
}
public class txtpassword
{
internal static object Text;
internal static bool Visible;
}
答
看看你的代码
con.Open();
Label4.Text ="ثبت نام با موفقیت انجام شد".ToString();
Label4.Visible = true;
con.Close();
com.ExecuteNonQuery();
您正在打开的连接,关闭它,然后执行。 它应该是:
con.Open();
Label4.Text ="ثبت نام با موفقیت انجام شد".ToString();
Label4.Visible = true;
com.ExecuteNonQuery();
con.Close();//close connection after execute.
也将是一个好主意,添加try/catch块周围 - 不能明确开
提到的问题的情况下连接。你能连接到数据库吗?代码保存到数据库的位置在哪里?什么是存储过程的名称。您的问题非常广泛,我们无法进行调试。所以请具体说明。 – MKR
“Strlogin”是我使用 检查数据库登录的例程的名称而且我创建了一个数据库,而且你也可以看到表格 事情是我没有任何想法“string strcon” Works and我写错了! – ali