做了一个SQL类但没有工作 [英] made a sql class but not working

查看:63
本文介绍了做了一个SQL类但没有工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个sql类,我将保留我项目的sql查询。

sql类如下:

I have made a sql class where I will keep sql queries of my project.
And the sql class is given below:

class sql
    {
        Form2_Search_Word SW = new Form2_Search_Word();

        BindingManagerBase bindingManager;
        DataTable dt;
        string conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\QuranDBdetails.accdb";

        public void KanzESearch(string kanztext)
        {
            OleDbConnection connection = new OleDbConnection(conStr);
            string searchsql = "Select Juz_Surah,Ayat_No,Surah_Ayat,[Kanz-ul-Iman_English] from Quran WHERE  Surah_Ayat LIKE ''%" + kanztext + "%'';";
            connection.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(searchsql, connection);
            dt = new DataTable();
            da.Fill(dt);
            connection.Close();
            SW.dataGridView1.DataSource = dt;
            //bindingManager = this.BindingContext[dt];
            string count = "select Count(*) from Quran WHERE Surah_Ayat LIKE ''%" + kanztext + "%'';";
            connection.Open();
            OleDbCommand command = new OleDbCommand(count, connection);
            // command.CommandText = searchsql;
            SW.textBox4.Text = command.ExecuteScalar().ToString();
            connection.Close();
        }

    }





现在我在我的Main表单类中调用这个KanzESearch函数但是它不工作



Now I call this KanzESearch function in my Main form class but it is not working

public partial class Form2_Search_Word : Form
    {


private void button1_Click(object sender, EventArgs e)//search Button
        {

sql queries = new sql();


            try
            {
                if (radioButton1.Checked == true) //arabic/urdu
                {
                    if (textBox1.Text != "")
                    {

                    }
                    else
                    {
                        MessageBox.Show("Must Enter a Word or Phrase", "Enter Text Please!");
                    }


                }
                else if (radioButton2.Checked == true)//English Search
                {
                    if (textBox1.Text != "")
                    {
                        if(rbtransKanzE.Checked==true)
                        {
                            string searchtext = textBox1.Text.ToString(); 
                            queries.KanzESearch(searchtext);  //here I am calling it …
                        }
                    }
                    else
                    {
                        MessageBox.Show("Must Enter a Word or Phrase", "Enter Text Please!");
                    }
                
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"Error!");
            }

}





当我点击我的搜索按钮时

它没有显示任何东西。请帮助

和我的sql类bindingContext也无法正常工作。

当我把它放在按钮后面时,同样的sql类代码可以正常工作。



When I click the my search button
it does not show any thing. Help please
and in my sql class bindingContext also not working.
The same sql class code does work when I put it in the back of button.

推荐答案

您正在 sql 类中实例化 Form2_Search_Word 对象,并且你的 Form2_Search_Word 类的 button1_Click 方法中的 sql 对象。你似乎对这些部件应该如何组合在一起感到有些困惑。



另请查看以下代码行:

You are instantiating a Form2_Search_Word object inside your sql class, and a sql object inside the button1_Click method of your Form2_Search_Word class. You seem to be somewhat confused about how all the pieces are supposed to fit together here.

Also take a look at these lines of code:
if (textBox1.Text != "")
{

}
else
{
    MessageBox.Show("Must Enter a Word or Phrase", "Enter Text Please!");
}



为什么不只是:


Why not just:

if (textBox1.Text == "")
{
    MessageBox.Show("Must Enter a Word or Phrase", "Enter Text Please!");
}


这里有几件事,但我们无法解决你的主要问题 - 你必须自己做。



但首先,为您提供几个方法:

1)您无需在文本字段(或任何其他字段)上调用ToString string数据类型):

There are a couple of things here, but we can''t solve your main problem - you will have to do that yourself.

But first, a couple of cahnges for you:
1) You do not need to call ToString on Text fields (or any other string datatype):
string searchtext = textBox1.Text.ToString();
queries.KanzESearch(searchtext);  //here I am calling it …

当你这样做像这样的事情,你只是表明你没有想过你在做什么。除了浪费打字和处理时间外,它什么都不做。

2)有一些有用的String类静态方法,值得你关注,而不是:

When you do things like this you are just showing that you haven''t thought about what you are doing. It does nothing at all, except to waste typing and processing time.
2) There are a couple of useful String class static methods which it is worth your looking at, Instead of:

if (textBox1.Text != "")

尝试

Try

if (!string.IsNullOrEmpty(textBox1.Text))



Or

if (!string.IsNullOrWhitespace(textBox1.Text))

(仅限.NET 4.0及以上版本)

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



完成此操作后,您可以查看问题的根源。

设置断点Button Click事件处理程序的第一行:

(.NET 4.0 and above only)
3) Do not 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 have done this, you can look at finding the source of your problem.
Put a breakpoint on the first line of your Button Click event handler:

sql queries = new sql();

并运行您的程序。填写文本框,然后按按钮。当调试器到达断点时,调试器将停止执行。

现在,单步执行程序并查看控件的位置 - 您可以进入 KanzESearch 方法并在程序执行时查看变量 - 并在每个阶段计算出在执行行之前您希望发生的事情。它会以应有的方式发生吗?没有?然后你可以看看为什么不,并自己解决问题,或者你会有更好的信息回到我们这里。



目前,我们没有信息,没有你的数据库(我们没有或没有),我们无法检查你的代码来获取信息。所以试试吧,让我们知道你发现了什么。

And run your program. Fill in your text box, and press the button. The debugger will stop executing when it hits the breakpoint.
Now, single step through your program and see where control goes - you can step into your KanzESearch method and look at the variables as the program executes - and at each stage work out what you expect to happen before you execute the line. Does it happen the way it should? No? Then you can look at why not, and either fix the problem yourself, or you will have much better information to come back to us with.

At the moment, we have no information, and without your database (which we don''t have or want) we can''t check your code to get information. So try it, and let us know what you find.


这篇关于做了一个SQL类但没有工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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