本文介绍了无法连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp.net的初学者,我无法使用此代码将数据从文本框发送到数据库,请帮帮我

这是代码
所有的领域都是品格


Iam a beginner in asp.net and i cannot send data from text box to database using this code please help me

this is code
all feilds are character


    protected void enterdata(object sender, EventArgs e)
    {
        try
        {
   
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Sree\\Documents\\krishna.accdb;Persist Security Info=True");
            con.Open();
            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO masterusertable (email ,password) Values (@UN, @PW)", con);
            oleDbCommand1.Parameters.AddWithValue("@UN", txtusername.Text);
            oleDbCommand1.Parameters.AddWithValue("@PW", txtpassword.Text);
            oleDbCommand1.ExecuteNonQuery();
            con.Close();
           }
        catch (Exception exp)
        {
            Response.Write(exp);
        }
    }



[edit]已添加代码块-OriginalGriff [/edit]



iam遇到异常


System.Data.OleDb.OleDbException(0x80040E14):INSERT INTO语句中的语法错误.在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,Object& executeResult)在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(System.Data.OleDb.OleDbCommand.ExecuteCommandResult(Object)处在Newreg.enterdata(Object)的System.Data.OleDb.OleDbCommand.ExecuteNonQuery()的System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior行为,String方法)处的Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior行为,Object& ExecuteResult)发件人,EventArgs e)



[edit]Code block added - OriginalGriff[/edit]



iam getting an exception


System.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at Newreg.enterdata(Object sender, EventArgs e)

推荐答案


OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO user (email ,password) Values (@UN, @PW)", con);
oleDbCommand1.Parameters.AddWithValue("@UN", txtusername.Text);
oleDbCommand1.Parameters.AddWithValue("@PW", txtpassword.Text);
oleDbCommand1.ExecuteNonQuery();



第三,切勿以明文形式存储密码-这是严重的安全隐患.这里有一个解释:密码存储:操作方法. [ ^ ]


我已经做过,但是发生了同样的异常"

啊!这可能很简单:将密码字段的名称括在方括号中:



Thirdly, never store passwords in clear text - it is a serious security risk. There is an explanation here: Password Storage: How to do it.[^]


"I had done it but same exception occurs"

Ah! It may be simple: enclose the name of your password field in square brackets:

OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO masterusertable (email ,password) Values (@UN, @PW)", con);

成为

OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO masterusertable (email ,[password]) Values (@UN, @PW)", con);

密码"是JET数据库中的保留字,也可能适用于ACE.

"Password" is a reserved word in JET databases, that may apply to ACE as well.


这篇关于无法连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:49