Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用文本框和搜索按钮C搜索数据库?_C#_Database_Data Binding - Fatal编程技术网

C# 如何使用文本框和搜索按钮C搜索数据库?

C# 如何使用文本框和搜索按钮C搜索数据库?,c#,database,data-binding,C#,Database,Data Binding,所以我想通过数据库搜索我输入的区号。我输入代码,然后在多行文本框中打印电话号码和区号下的持续时间。这就是我目前所拥有的。是的,我是一个初学者,也是一个差劲的人:D public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) {

所以我想通过数据库搜索我输入的区号。我输入代码,然后在多行文本框中打印电话号码和区号下的持续时间。这就是我目前所拥有的。是的,我是一个初学者,也是一个差劲的人:D

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'phonecallsDataSet1.Calls_with_Region' table. You can move, or remove it, as needed.
        this.calls_with_RegionTableAdapter.Fill(this.phonecallsDataSet1.Calls_with_Region);
        tbAreaCode.Text = String.Format("Phone Number" + "\t" + "\t" + "Duration" + "\r\n"
            + "============" + "\t" + "\t" + "=======");
    }

    private void btnSearchAC_Click(object sender, EventArgs e)
    {
        foreach (DataColumn number in phoneCallbindingSource.List)
        {
            if (txtAC.Text == ((int)number["Area Code"]))
            {
                tbAreaCode.Text += ((int)number["Phone Number"]); 
          }
        }
    }
}
}
错误


无法将带[]的索引应用于System.Data.DataColumn类型的表达式


我认为查询数据库本身以获取这些信息,并用返回的值填充文本框或列表框,是实现您想要实现的目标的更好方法。

好的,所以问题是您是按列而不是按行进行迭代。下面是一个工作示例,展示了两种方法来完成相同的事情。一次遍历DataTable是否因为有多个表而使用数据集?如果只有一个表,则使用DataTable;如果有多个表,则使用DataSet。第二个使用我输入的Linq,因为作为一个初学者,接触C的一些更有用的特性是很有帮助的。只需将其粘贴到控制台应用程序的主应用程序中,覆盖初始主功能,您就可以使用它了

static void Main(string[] args)
{
    string CurrentAreaCode = "415";// Input from textbox;

    // Setup Mock Dataset
    DataSet ds = new DataSet("Information");
    ds.Tables.Add("AreaCodeInformation");
    ds.Tables[0].Columns.Add("AreaCode");
    ds.Tables[0].Columns.Add("PhoneNumber");
    ds.Tables[0].Rows.Add();
    ds.Tables[0].Rows[0][0] = 415;
    ds.Tables[0].Rows[0][1] = 9252222222;

    // output with row iterator
    Console.WriteLine("Using Iteration of DataTable in DataSet");
    foreach (DataRow number in ds.Tables[0].AsEnumerable())
    {
        if (number["AreaCode"].ToString() == CurrentAreaCode)
        {
            Console.WriteLine(number["PhoneNumber"].ToString());
        }
    }
    Console.WriteLine("Press Enter To Continue...");
    Console.ReadLine();

    // output using Linq
    Console.WriteLine("Using Linq");
    Console.WriteLine((from info 
                       in ds.Tables["AreaCodeInformation"].AsEnumerable() 
                       where info.Field<string>("AreaCode") == CurrentAreaCode 
                       select info.Field<string>("PhoneNumber")).FirstOrDefault());
    Console.ReadLine();
}

您遇到了什么错误?无法将[]索引应用于System.Data.DataColumn类型的表达式我认为填充应说明此问题。phonecallsDataSet1.表[Calls_with_Region]数据集中是否确实有数据?另外,您在哪一行上得到错误信息?