C# 如何从数据库中获取数据并将其显示到另一个窗体?
帮助我如何从数据库获取数据并显示到另一个窗体? 这些照片是系统的流程图 流动 1.我将选择一行 2.另一个窗体显示我选择的特定行的开始日期和结束日期 这些是剩余的错误C# 如何从数据库中获取数据并将其显示到另一个窗体?,c#,mysql,C#,Mysql,帮助我如何从数据库获取数据并显示到另一个窗体? 这些照片是系统的流程图 流动 1.我将选择一行 2.另一个窗体显示我选择的特定行的开始日期和结束日期 这些是剩余的错误 在表单之间传递数据: 表格1 using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form
在表单之间传递数据: 表格1
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string nTitle = textBox1.Text;
string nText = textBox2.Text ;
Form2 frm = new Form2(nTitle, nText);
frm.Show();
}
}
}
表格2
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
}
public Form2(string title,string txt)
{
InitializeComponent();
this.Text = title;
label1.Text = txt;
}
}
}
这里有一个你可以做的选择 在
表单2上
:
创建一个标签
或一个文本框
,然后给它一个类似idtextbox.text的名称(或任何你想要的名称),或者只将标签
设置为idlabel
,然后将修饰符属性从private
设置为public
然后创建一个基于id从数据库加载数据的方法,如下所示:
public void yourMethodName()
{
string connection = @"datasource = localhost; database=sub; Uid=root; Password=steph";
MySqlConnection connect = new MySqlConnection(connection);
string yourQuery = null;
yourQuery = "SELECT top 1 * from sub.transmittal_transaction WHERE id=@id";
MySqlCommand executeQuery = new MysqlCommand(yourQuery, connect);
executeQuery.Parameters.Add("@id", SqlDbType.Int);
executeQuery.Parameters["@id"].Value = yourtextboxnamehere.Text; // or = your labelname.text
connect.Open();
MySqlDataReader datareader = executeQuery.ExecuteReader();
try
{
if (datareader.HasRows)
{
datareader.Read();
yourtextboxnamehere.Text = datareader["id"].ToString();
startDate.text = datareader["yourStartDateColumnNameHere"].ToString(); // if your using a label it still ends in .text same goes with using textbox to display it on the form.
endDate.text = datareader["yourEndDateColumnNameHere"].ToString(); // if your using a label it still ends in .text same goes with using textbox to display it on the form.
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
然后在Form1
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
Form2 frm = new Form2();
frm.yourtextboxnamehere.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
frm.ShowDialog();
}
}
上面的代码在Form1
上执行,cellcontextclick
事件将打开Form2
,然后将datagridview上当前行id
的值传递到Form2
标签或文本框
,然后从那里读取并显示startdate
和enddate
基于数据库中的id
。但是,如果您希望像我在图像上注意到的那样,每个页码有一个id
,那么您需要的不仅仅是这个代码
这也是一种选择:
executeQuery.Parameters.AddWithValue("@id", yourtextboxnamehere.Text);
而不是:
executeQuery.Parameters.Add("@id", SqlDbType.Int);
executeQuery.Parameters["@id"].Value = yourtextboxnamehere.Text;
但这种选择被认为是不好的。阅读本文的原因。当您可以直接从数据库调用数据时,为什么需要从两个表单传递数据?除非当两个表单中的一个表单上的值发生变化时有某些必要的条件。因为他说:如何从数据库中获取数据并准确地显示到另一个表单上!从数据库获取数据
不从其他表单获取数据,然后将值传递到此表单。True。现在向OP演示如何正确传递该值。:)让我们看看,你试过什么,什么不起作用。请发一些代码。事实上,我是这门语言的新手。我尝试了一些代码,但还没有完成。老实说,我不知道如何完成它。我尝试了你的代码,我认为这是我需要的代码,但还有一些错误,你能帮我擦亮这些代码错误吗。我会上传上面的截图。thanks@Stephii我编辑了代码,你确定你使用了正确的连接吗?如果您有MySqlConnection,则应该没有错误。我忘了提到您需要将文本框>属性>修饰符从private
设置为public
谢谢。我已经编辑了代码。表2中还有3个错误。我很乐意帮忙。只需显示剩余的错误。注意*:还要试着理解代码,而不仅仅是复制粘贴。我已经上传了上面的截图。是的,我检查代码。