У меня есть связь с локальной базой данных.Я хотел полное имя от (в данном случае) combobox4
до текстового поля textbox
.Он отображает это значение, но без пробела.Например, из combobox4
"S BOX" в textbox
означает только "S".Я пытался с textBox4.Text = string.Join(" ", parts[1]);
, но я не мог работать.Я поделился своим кодом: Insertstatus.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace KontrolaBazaDanych
{
public partial class InsertStatus : Form
{
string Zlecenie_wykonane;
string Zlecenie_rozpoczete;
MySqlConnection connection;
MySqlCommand command;
MySqlDataReader dr;
Form1 frm1 = (Form1)Application.OpenForms["Form1"];
public InsertStatus()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
string InsertQuery = "INSERT INTO projekt1.status_zl(ID_ZLECENIA, ID_PRACOWNIKA, ID_MODULU, ID_PROJEKTU, ILOSC_PRODUKTOW, DATA_ROZPOCZECIA, DATA_ZAKONCZENIA, ZLECENIE_WYKONANE, ZLECENIE_ROZPOCZETE) VALUES('" + textBox7.Text + "','" + textBox9.Text + "','" + textBox8.Text + "','"+ textBox10.Text +"','" + numericUpDown4.Value.ToString() + "','" + dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss") + "','" + dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss") + "','" + Zlecenie_wykonane + "','" + Zlecenie_rozpoczete + "')";
connection.Open();
MySqlCommand command = new MySqlCommand(InsertQuery, connection);
if (command.ExecuteNonQuery() == 1)
{
MessageBox.Show("Dane są dodane.");
}
else
{
MessageBox.Show("Błąd.");
}
frm1.loaddata4();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
numericUpDown4.Value = 0;
dateTimePicker1.Value = DateTime.Now;
dateTimePicker2.Value = DateTime.Now;
checkBox1.Checked = false;
checkBox2.Checked = false;
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Zlecenie_wykonane = "1";
Zlecenie_rozpoczete = "0";
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
Zlecenie_wykonane = "0";
Zlecenie_rozpoczete = "1";
}
private void InsertStatus_Load(object sender, EventArgs e)
{
connection = new MySqlConnection();
connection.ConnectionString = "datasource=localhost;port=3306;username=root;password=";
connection.Open();
string selectQuery2 = "SELECT * FROM projekt1.pracownicy";
MySqlCommand command2 = new MySqlCommand(selectQuery2, connection);
MySqlDataReader reader2 = command2.ExecuteReader();
while (reader2.Read())
{
comboBox1.Items.Add(reader2.GetString("ID_PRACOWNIKA") + " " + reader2.GetString("IMIE") + " " + reader2.GetString("NAZWISKO"));
}
connection.Close();
connection.Open();
string selectQuery3 = "SELECT * FROM projekt1.moduly";
MySqlCommand command3 = new MySqlCommand(selectQuery3, connection);
MySqlDataReader reader3 = command3.ExecuteReader();
while (reader3.Read())
{
comboBox2.Items.Add(reader3.GetString("NAZWA"));
}
connection.Close();
connection.Open();
string selectQuery4 = "SELECT * FROM projekt1.zlecenia";
MySqlCommand command4 = new MySqlCommand(selectQuery4, connection);
MySqlDataReader reader4 = command4.ExecuteReader();
while (reader4.Read())
{
comboBox3.Items.Add(reader4.GetString("NUMER_ZLECENIA"));
}
connection.Close();
connection.Open();
string selectQuery5 = "SELECT * FROM projekt1.projekty";
MySqlCommand command5 = new MySqlCommand(selectQuery5, connection);
MySqlDataReader reader5 = command5.ExecuteReader();
while (reader5.Read())
{
comboBox4.Items.Add(reader5.GetString("ID") + " " + reader5.GetString("NAZWA_PROJEKTU"));
}
connection.Close();
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
textBox5.Enabled = false;
textBox6.Enabled = false;
textBox7.Enabled = false;
textBox8.Enabled = false;
textBox9.Enabled = false;
textBox10.Enabled = false;
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
command = new MySqlCommand("SELECT * FROM projekt1.moduly WHERE NAZWA='"+ comboBox2.Text +"'", connection);
connection.Open();
command.ExecuteNonQuery();
MySqlDataReader dr;
dr = command.ExecuteReader();
while (dr.Read())
{
string nazwa = (string)dr["NAZWA"].ToString();
textBox3.Text = nazwa;
string id_modulu = (string)dr["ID_MODULU"].ToString();
textBox8.Text = id_modulu;
}
connection.Close();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
command = new MySqlCommand("SELECT * FROM projekt1.zlecenia WHERE NUMER_ZLECENIA='" + comboBox3.Text+"'", connection);
connection.Open();
command.ExecuteNonQuery();
dr = command.ExecuteReader();
while (dr.Read())
{
string opis = (string)dr["OPIS_ZLECENIA"].ToString();
textBox5.Text = opis;
string numer = (string)dr["NUMER_ZLECENIA"].ToString();
textBox6.Text = numer;
string status_zl = (string)dr["ID_ZLECENIA"].ToString();
textBox7.Text = status_zl;
}
connection.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != -1)
{
string[] parts = comboBox1.Items[comboBox1.SelectedIndex].ToString().Split(' ');
textBox9.Text = parts[0];
textBox1.Text = parts[1];
textBox2.Text = parts[2];
}
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox4.SelectedIndex != -1)
{
string[] parts = comboBox4.Items[comboBox4.SelectedIndex].ToString().Split(' ');
textBox10.Text = parts[0];
textBox4.Text = string.Join(" ", parts[1]);
}
}
}
}
Я не могу найти решение, которое может быть эффективным.Речь идет о string.Join
строительстве или о создании общественного класса?Есть ли у кого-нибудь идеи, как это решить или объяснить на примере кода?Привет.