如何查询数据库并在winforms中显示信息?

时间:2015-05-30 09:20:02

标签: c# winforms

我有一个Patient数据库实体。 我想使用给定的Patient查询并检查id记录是否已存在。

结果(例如 that id already exists )应存储在 textBox1 中并呈现给用户。

这是我到目前为止所得到的:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace LMS {
    public partial class PIDD: Form {
        public PIDD() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            this.Close();
            frmMed Med = new frmMed();
            Med.Show();
        }

        private void textBox1_TextChanged(object sender, EventArgs e) {

        }
    }
}

我应该如何从这一点继续?

1 个答案:

答案 0 :(得分:0)

如果您使用的是mssql,请尝试此操作。

            var connection = new SqlConnection("ConnectionString");
            connection.Open();
            var command = new SqlCommand("SELECT id FROM Table WHERE id='" + textBox2.Text + "",connection);
            var reader = command.ExecuteReader();
            if (reader.Read())
            {
                textBox1.Text = reader["id"].ToString();
            }
            else
            {
                ///No entry found
            }
            connection.Close();