emir
Goto Top

C-Sharp - In Datenbank suchen und Ergebnis in DataGridView ausgeben

.Net 2.0
Datenbank - MS SQL Server 2005

Hallo an alle...
hab da mal eine Frage...Könnte einer von euch mir erklären, wie ich mit einem Textfeld und Button eine Suche realisieren kann? Die Ausgabe der Datenbank funktioniert schon...aber bei der Suche habe ich meine Probleme....Code Beispiel wäre nett.

Mit freundlichen Grüßen
Timur


Der Code bisher:

using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DB_Abfrage
{
public partial class Form1 : Form
{
private SqlConnection sqlconnection = new SqlConnection();
private string connectionstring;
public Form1()
{
connectionstring = System.Configuration.ConfigurationSettings.AppSettings["connectionstring"];
InitializeComponent();
online_offline_status();
}

private void button1_Click(object sender, EventArgs e)
{
sqlconnection.ConnectionString = connectionstring;
sqlconnection.Open();
online_offline_status();
}

private void closeconn()
{
if (sqlconnection.State != ConnectionState.Closed)
sqlconnection.Close();
online_offline_status();
}

private void button2_Click(object sender, EventArgs e)
{
closeconn();

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
closeconn();
}

private void button3_Click(object sender, EventArgs e)
{
if (sqlconnection.State != ConnectionState.Closed)
{
string sql = "SELECT * FROM t_user";
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, sqlconnection);
DataTable datatable = new DataTable();
dataadapter.Fill(datatable);
if (datatable.Rows.Count > 0)
{

dataGridView1.DataSource = datatable;
}
}
else
{
MessageBox.Show("Keine Verbindung zur DB hergestellt!");
closeconn();
}
}

private void in_db_search_button_Click(object sender, EventArgs e)
{

}
//Show On/Offline Status of the SQL-connection
private void online_offline_status()
{
if (sqlconnection.State != ConnectionState.Closed)
{
Status_Label.ForeColor = Color.Green;
Status_Label.Text = "Online";
}
else
{
Status_Label.ForeColor = Color.Red;
Status_Label.Text = "Offline";
}
}
}
}

Content-Key: 69529

Url: https://administrator.de/contentid/69529

Ausgedruckt am: 28.03.2024 um 20:03 Uhr