如何使用Oop方法创建基于名称搜索数据的编码 [英] How Do I Create Coding For Searching Data Based On Name With Oop Method

查看:85
本文介绍了如何使用Oop方法创建基于名称搜索数据的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在使用visual studio 2008和microsoft SQL server 2008创建一个处理学生成绩的程序,我想要做的是创建一个搜索按钮。

该程序包含:

用SQL创建的数据库

windows窗体

类连接SQL和windows窗口



我要创建的函数是:

当我在文本框中输入学生的姓名并单击表单中的搜索按钮时,它将显示datagridview上的所有信息。



我该怎么办?

hello, i'm using visual studio 2008 and microsoft SQL server 2008 to create a program for processing student grade, what i want to do is to create a search button.
the program consist of:
database created with SQL
windows form
class to connect SQL and windows form

the function that i want to create is this:
when i type the name of a student in the textbox and clicked the search button in the form it will display all the information on datagridview.

how do i do that?

推荐答案

你可以尝试这段代码。如果出现任何问题,请通过评论回复。确保在类中使用处理数据操作的名称空间。在SQL Select查询和数据适配器的Fill方法中使用适当的表和数据库名称而不是test和master。把这个方法放在你的班级。并从你的form.cs调用它。



You can try this code. Do reply by commenting if any problem occurs. Make sure to use the namespaces in your class that handles data operations. Use appropriate table and database name instead of "test" and "master" in SQL Select query and in Fill method of data adapter. Put this method in your class. And invoke it from your form.cs

using System.Data;
using System.Data.SqlClient;

DataSet getDataSet(string Command, string Connection)
{
    SqlDataAdapter sda = new SqlDataAdapter(Command, Connection);
    DataSet ds = new DataSet();
    sda.Fill(ds, "test");
    return ds;
}

private void BGetData_Click(object sender, EventArgs e)
{
    dataGridView1.DataSource = getDataSet("SELECT * FROM test WHERE Name='" + TBName.Text + "'", @"Server=.\SQLEXPRESS; Database=master; Integrated Security=true;").Tables["test"];
}


这篇关于如何使用Oop方法创建基于名称搜索数据的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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