运行应用程序时出错 [英] Error When running application

查看:96
本文介绍了运行应用程序时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A使用字段创建了MS Access文件,并设计了C#表单以将数据插入这些字段.这是为在数据库中插入文本框字段而编写的代码块.

A created a MS Access file with fields and designed a C# form to insert data into these fields. This is the block of code written to insert a textbox field to the database.

private void button1_Click(object sender, EventArgs e)
        {
            string insertCommand = "INSERT into tblContact (First Name) " + " VALUES( + fntxtbx.Text + )";

            OleDbConnection datacon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\CROSSLINK\\Documents\\Visual Studio 2008\\Projects\\contact1.mdb");

            OleDbCommand dbcommand = new OleDbCommand( insertCommand, datacon );
                       
            dbcommand.Connection = datacon;
            datacon.Open();
            
            OleDbDataAdapter b = new OleDbDataAdapter();

            DataSet dset = new DataSet();

            b.Fill(dset);
            datacon.Close();
        }


当我尝试运行该应用程序时,出现以下未处理的异常"错误...


When I tried to running the application the following "unhandled Exception" error appeared...

Unhandled exception has occurred in your application. If you click....
The SelectCommand property has not been initialized before calling "Fill"


请问如何解决这个问题?


Please how can I resolve this issue? Do I have to keep the access file running before this can work or what?

推荐答案

问题是您创建了OleDbDataAdapter,但尚未分配OleDbCommand.
之后;
The problem is that you have created the OleDbDataAdapter but you haven''t assigned the OleDbCommand to it.
After;
OleDbDataAdapter b = new OleDbDataAdapter();


放;


Put;

b.SelectCommand = dbcommand ;


或者您可以将其写为;


or you can write it as;

b.SelectCommand = new OleDbCommand(insertCommand, datacon );



并将您的查询更改为;



And change your query to;

string insertCommand = "INSERT into tblContact (First Name) VALUES('" + fntxtbx.Text.Replace("'","''") + "')";


否则,任何带有撇号的名称都将导致错误,并可能引发Sql注入攻击.


Otherwise any names with an apostrophe will cause errors and open things up for a Sql injection attack.




试试这个:

Hi,

try this:

private void button1_Click(object sender, EventArgs e)
{
  string insertCommand = "INSERT into tblContact (First Name) " + " VALUES( + fntxtbx.Text + )";

  OleDbConnection datacon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\CROSSLINK\\Documents\\Visual Studio 2008\\Projects\\contact1.mdb");

  OleDbCommand dbcommand = new OleDbCommand( insertCommand, datacon );

  dbcommand.Connection = datacon;
  datacon.Open();

  OleDbDataAdapter b = new OleDbDataAdapter();
  b.SelectCommand = dbcommand;
  DataSet dset = new DataSet();

  b.Fill(dset);
  datacon.Close();
}



您忘记设置已经创建的"SelectCommand"



You forget to set the "SelectCommand" that you had already createdd


这篇关于运行应用程序时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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