记录不在ms访问数据库中插入 [英] Record not insert in ms access database

查看:68
本文介绍了记录不在ms访问数据库中插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ms访问数据库中通过asp.net c#插入数据。

我的程序运行成功,但记录没有插入数据库

请帮助...



我尝试过的事情:



i am inserting data by asp.net c# in ms access database.
my program run successfully but records not insert in data base
please help...

What I have tried:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
                     OleDbConnection con = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();
            
            con.ConnectionString=@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\manish\Desktop\myprogram\demo.accdb;User ID=admin";
            cmd.Connection = con;
            string sql="insert into data1(name,lastname)values(" + txtfirstname.Text + "," + txtlastname.Text + ")";
            try{

               con.Open();
               cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch(Exception)
            {
                con.Close();

            }
        }
    }
}

推荐答案

您的代码 SQL注入 [ ^ ]易受攻击。

您应该使用参数化查询 [ ^ ]而不是字符串连接查询!以下是示例代码: OleDbCommand.Parameters属性(System.Data.OleDb) [ ^ ]



另一方面,你的连接字符串似乎是错误的。请检查:访问连接字符串 - ConnectionStrings.com [ ^ ]
Your code is SQL Injection[^] vulnerable.
You should use parameterized queries[^] instead of string-concatenating query! Here is a sample code: OleDbCommand.Parameters Property (System.Data.OleDb)[^]

On the other hand, your connection string seems to be wrong. Please, check this: Access connection strings - ConnectionStrings.com[^]


1)不要硬编码连接字符串:它总会回来咬你后来。始终使用配置文件来存储它们。这样,当您向其他人发布代码时,您可以根据需要更改它们。



2)始终将连接,命令等对象视为稀缺资源并在处理时将其丢弃你完成了它们。一个使用块是最简单的方法,因为它会在对象超出范围时自动调用Dispose。



)永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

1) Do not hard code connection strings: it will always come back to bite you later. Always use a configuration file to store them. That way you can change them as necessary when you release code to other people.

2) Always treat connection, command and suchlike objects as scarce resources and Dispose of them when you are finished with them. A using block is the simplest way to do this as it automatically calls Dispose whenteh object goes out of scope.

3) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



4)永远不要吞下异常 - 这意味着解决问题所需的基本数据会被丢弃 - 意味着永远不要使用它:

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

4) Never swallow exceptions - it means that the essential data you need to fix the problem is thrown away - that means never using this:

catch(Exception)

因为你没有访问异常的内容,以及告诉您系统失败原因的消息。相反,使用它:

because you have no access to what the exception was, and the message which tells you why the system failed. Instead, use this:

catch(Excpetion ex)

并使用记录甚至是MessageBox来显示Exception对象包含的错误消息。这样,你就可以找出问题所在,并且可以修复它......

and use logging or even a MessageBox to display the error message the Exception object contains. That way, you get to find out what the problem is, and can fix it...


这篇关于记录不在ms访问数据库中插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆