如何将ms访问数据库显示为c#.net(帮助我)错误 [英] How to display ms access database to c#.net (help me) error

查看:51
本文介绍了如何将ms访问数据库显示为c#.net(帮助我)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void AcNotextBox_TextChanged(object sender, EventArgs e)
        {
            string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;

            string cmdString = "SELECT * FROM tblBasicInfo WHERE ID =" + int.Parse(AcNotextBox.Text);

            using (OleDbConnection conn = new OleDbConnection(connString))
            {
                using (OleDbCommand cmd = new OleDbCommand(cmdString, conn))
                {
                    conn.Open();

                    OleDbDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        NametextBox.Text = (reader["SName"].ToString());
                        FNametextBox.Text = (reader["FName"].ToString());
                        AddresstextBox.Text = (reader["Address"].ToString());
                        CelltextBox.Text = (reader["Cell"].ToString());
                        NationalitytextBox.Text = (reader["Nationality"].ToString());
                        ReligiontextBox.Text = (reader["Religion"].ToString());
                        FCNICNotextBox.Text = (reader["FCNICNo"].ToString());
                        FatherOccuptextBox.Text = (reader["FOccup"].ToString());
                        TransportFacilitytextBox.Text = (reader["TranFacility"].ToString());
                        InFiguredateTimePicker.Text = (reader["DOB"].ToString());
                        DateofAdmdateTimePicker.Text = (reader["DateofAdm"].ToString());
                        RegistrationNotextBox.Text = (reader["RegNo"].ToString());
                        TranFeetextBox.Text = (reader["TransportChr"].ToString());
                        SRemarkstextBox.Text = (reader["SpecialRemarks"].ToString());
                        ClassofcomboBox.Text = (reader["ClassofAdm"].ToString());
                        ClassOfReadcomboBox.Text=(reader["ClassofReading"].ToString());





我尝试过:



为MS Access数据表文本框调用数据是填充但按退格键为我打开一个消息





What I have tried:

Calling data for MS Access Data Table text box is fill but press backspace to open a msg for me

FormatException was unhandled<br />
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll





附加信息:输入字符串不在正确的格式。



Additional information: Input string was not in a correct format.

推荐答案

我们无法分辨 - 这将取决于您的数据以及您的代码,我们无权访问。

所以,这取决于你。

幸运的是,你有一个工具可以帮助你找到正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
We can't tell - this is going to depend on your data as well as your code, and we dont have access to that.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


我想你的麻烦的原因是在下面一行:

I guess that the reason of your trouble is in below line:
DateofAdmdateTimePicker.Text = (reader["DateofAdm"].ToString());





您必须知道设置 DateTimePicker [ ^ ]未设置值。

另一方面, DateofAdm 字段可以是 Null



所以,你必须检查DateofAdm是否不是null,而不是set DateTimePciker的值。



You have to know that setting a Text property of DateTimePicker[^] does not set a value.
On the other hand a DateofAdm field can be Null.

So, you have to check if DateofAdm is not null, than set a value for DateTimePciker.

DateofAdmdateTimePicker.Value = DBNull.Value.Equals(reader["DateofAdm"]) ? DateofAdmdateTimePicker.MinDate : reader["DateofAdm"];





详情请见:

使用DateTimePicker控件 [ ^ ]

DBNull.Value字段(系统) [ ^ ]


最可能的原因是这一行:
The most likely cause is this line:
string cmdString = "SELECT * FROM tblBasicInfo WHERE ID =" + int.Parse(AcNotextBox.Text);



无法保证用户是什么已键入该框可以转换为整数。您需要使用 int.TryParse [ ^ ]尝试解析该值,并向用户报告错误它无法解析。



然而,你不应该这样做。虽然您的代码避免了 SQL注入 [ ^ ]漏洞 in这个特定情况(因为参数已被解析为 int ),您仍应使用参数化查询。通过允许DBMS缓存查询计划,它可以提高性能;对于对SQLi免疫的案例而言,这是一个好习惯。




There is no guarantee that what the user has typed into that box can be converted to an integer. You need to use int.TryParse[^] to attempt to parse the value, and report an error to the user if it can't be parsed.

However, you shouldn't be doing it like that anyway. Whilst your code avoids a SQL Injection[^] vulnerability in this specific case (since the parameter has been parsed as an int), you should still use a parameterized query. It will potentially improve performance by allowing the DBMS to cache the query plan; and it's a good habit to get into for cases which aren't immune to SQLi.

int id;
if (!int.TryParse(AcNotextBox.Text, out id))
{
    // TODO: Report the problem to the user
    return;
}

string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
string cmdString = "SELECT * FROM tblBasicInfo WHERE ID = ?";

using (OleDbConnection conn = new OleDbConnection(connString))
using (OleDbCommand cmd = new OleDbCommand(cmdString, conn))
{
    cmd.Parameters.AddWithValue("ID", id);
    
    conn.Open();
    using (OleDbDataReader reader = cmd.ExecuteReader())
    {
        if (reader.Read())
        {
            ...
        }
    }
}




您想知道的一切SQL注入(但不敢问)|特洛伊亨特 [ ^ ]

如何在没有技术术语的情况下解释SQL注入? |信息安全堆栈交换 [ ^ ]

查询参数化备忘单| OWASP [ ^ ]



Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]


这篇关于如何将ms访问数据库显示为c#.net(帮助我)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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