我试图创建一个将在数据库中执行的命令,但是当我尝试执行它时,我总是得到相同的错误。
首先,让我们从代码开始,也许有些问题我无法看到:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
public partial class ASPX_register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string username = Request.Form["username"].Replace("'","''");
string password = Request.Form["password"].Replace("'", "''");
string fname = Request.Form["fname"].Replace("'", "''");
string lname = Request.Form["lname"].Replace("'", "''");
string email = Request.Form["email"].Replace("'", "''");
OleDbConnection dbCon = new OleDbConnection();
OleDbCommand dbCmd = new OleDbCommand();
String Path = Server.MapPath(@"../App_Data/ShakedDB.mdb;");
dbCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Path;
dbCmd.Connection = dbCon;
OleDbParameter pUser = new OleDbParameter();
OleDbParameter pPass = new OleDbParameter();
OleDbParameter pEmail = new OleDbParameter();
OleDbParameter rDate = new OleDbParameter();
OleDbParameter pfname = new OleDbParameter();
OleDbParameter plname = new OleDbParameter();
pUser.ParameterName = "@Username";
pUser.Value = username;
pPass.ParameterName = "@Password";
pPass.Value = password;
pfname.ParameterName = "@pfname";
pfname.Value = fname;
plname.ParameterName = "@plname";
plname.Value = lname;
pEmail.ParameterName = "@Email";
pEmail.Value = email;
rDate.ParameterName = "@RDate";
rDate.Value = DateTime.Now.ToString();
dbCmd.Parameters.Add(pUser);
dbCmd.Parameters.Add(pPass);
dbCmd.Parameters.Add(pEmail);
dbCmd.Parameters.Add(rDate);
dbCmd.Parameters.Add(pfname);
dbCmd.Parameters.Add(plname);
OleDbDataAdapter dataA = new OleDbDataAdapter(dbCmd);
dbCmd.CommandText = String.Format("SELECT * FROM Members WHERE username = @Username");
DataTable dataT = new DataTable();
dataA.Fill(dataT);
if (dataT.Rows.Count == 0)
{
dbCmd.CommandText = String.Format("INSERT INTO Members (username,[password],[firstName],[lastName],email,registerDate) VALUES (@Username,@Password,@pfname,@plname,@Email,@RDate);");
dbCon.Open();
dbCmd.ExecuteNonQuery();
dbCon.Close();
Response.Write(" " + fname + " " + lname);
Response.Write("נרשמת לאתר בהצלחה" + "<br />");
Response.Write("שם המשתמש שלך הוא:" + username + "<br />");
Response.Write("הסיסמא שלך היא:" + password + "<br />");
Response.Write("האימייל שאיתו נרשמת הוא:" + email + "<br />");
Response.Write("<a href='../ASPX/main.aspx' target='mainFrame'>בחזרה לעמוד הראשי לחץ כאן</a>");
}
else
{
Response.Write("שם המשתמש הזה כבר תפוס. נסה שם אחר." + "<br />");
Response.Write("<a href='main.aspx' target='mainFrame'>בחזרה לעמוד הראשי לחץ כאן</a>");
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
}
请忽略希伯来语部分,它只是告诉用户他注册了。 这是我得到的错误:http://prntscr.com/6gh189 希伯来语部分说&#34;表达式数据类型标准不匹配&#34;。 我想说,如果没有&#39; pfname&#39;和&#39; plname&#39;参数,由于某种原因,当我添加它们时会发生此错误。 我也尝试写这个&#39;插入&#39;命令它工作,所以它必须是参数的东西,但我找不到它:
INSERT INTO Members (username,[password],[firstName],[lastName],email,registerDate) VALUES (@Username,@Password,'asdfasdfasdf','asdfasdf',@Email,@RDate);
上面的代码工作,如果我使用它我没有错误。
答案 0 :(得分:1)
您正在使用OleDbParameter Constructor (String, Object)
重载
DateTime rDate = new DateTime();
rDate = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyy hh:mm:ss"));
...
dbCmd.Parameters.Add(rDate);
答案 1 :(得分:0)
"SELECT * FROM Members WHERE username = @Username"
变为:
"SELECT * FROM Members WHERE username = ?"
在设置commandtext后移动参数(并将所有内容设置在一行中):
dbCmd.Parameters.Add("@Username", OleDbType.VarChar, 15).Value = username;
对于插入尝试类似这样的事情:
"INSERT INTO Members (username,[password],[firstName],[lastName],email,registerDate) VALUES (?,?,?,?,?,?);"
cmd.Parameters.AddWithValue("UserName", username);
cmd.Parameters.AddWithValue("Password", password);
etc.
这就是访问dbs的方式。