如何将数据从MySQL放到C# [英] how to put data from MySQL to C#

查看:95
本文介绍了如何将数据从MySQL放到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿我已经完成了搜索方法,我想做的就是将数据放入文本框。





例如,我需要使用ID从数据库中提取数据并在所需文本框中设置其他信息,用户在文本框中提供ID。



感谢您的帮助我...... :))GOD BLESS!





我的代码.....



hey I'm already done for the searching method and all i want to do is to put the data into the textbox.


For example I need pull data from DB using ID and set other info in required text box, ID is provided by user in a Text Box.

thanks for helping me.. :)) GOD BLESS!


my code.....




使用System;

使用System.Collections.Generic ;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System .Linq;

使用System.Text;

使用System.Windows.Forms;

使用MySql.Data.MySqlClient;

命名空间WindowsFormsApplication17.Forms

{

公共部分类Form7:表格

{

公共Form7 ()

{

InitializeComponent();

}



private void btnAdd_Click(object sender,EventArgs e)

{

string input = txtStudID.Text.Trim( );

string conn =server = localhost; user = root; password = 1; database = record;;

MySqlConnection myconn = new MySqlConnection(conn);

string sql =SELECT`CudeteID` FROM`Information` WHERE`CandryID` ='+ input +';;

MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);

MySqlDataReader阅读器;

DataTable dt = new DataTable();

da.Fill(dt);





if(dt.Rows.Count == 0)

{

MessageBox.Show (输入+不存在。,不存在);

txtStudID.Focus();

txtStudID.Text =;



}

其他

{

//这对于将显示列数据的文本框..

txtStudName.Text = dt.Columns [0] +;

txtAdd.Text = dt.Columns +address;

txtContact.Text = dt.Columns +Contact;







}

}



private void btnClose_Click(object sender,EventArgs e)

{

Form1 form1 = new Form1();

form1.Show();

this.Hide();



}

}

}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication17.Forms
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}

private void btnAdd_Click(object sender, EventArgs e)
{
string input = txtStudID.Text.Trim();
string conn = "server=localhost;user=root;password=1;database=record;";
MySqlConnection myconn = new MySqlConnection(conn);
string sql = "SELECT `StudentID` FROM `Information` WHERE `StudentID` = '" + input + "';";
MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
MySqlDataReader reader;
DataTable dt = new DataTable();
da.Fill(dt);


if (dt.Rows.Count == 0)
{
MessageBox.Show(input + " does not exist.", "Not Exists");
txtStudID.Focus();
txtStudID.Text = "";

}
else
{
//this for textboxes that will display the data from column..
txtStudName.Text = dt.Columns[0] + "";
txtAdd.Text = dt.Columns + "address";
txtContact.Text = dt.Columns + "Contact";



}
}

private void btnClose_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show();
this.Hide();

}
}
}

推荐答案





Hi,

txtAdd.Text = dt.Columns + "address";
txtContact.Text = dt.Columns + "Contact";





这部分对我来说有点混乱。



你应该写代码这样,







This part is bit confusing for me apart from that.

you should write the code in this way,


string input = txtStudID.Text.Trim();
// This a very bad thing to put Connection string with in the code.
string conn = "server=localhost;user=root;password=1;database=record;"; 

MySqlConnection myconn = new MySqlConnection(conn);
// Change Field Name as you have in your Table
string sql = "SELECT `StudentAdd`,`StudentName`,`StudentContact` FROM `Information` WHERE `StudentID` = '" + input + "';";
MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
MySqlDataReader reader;
DataTable dt = new DataTable();
da.Fill(dt);

 
if (dt.Rows.Count == 0)
{
MessageBox.Show(input + " does not exist.", "Not Exists");
txtStudID.Focus();
txtStudID.Text = "";
 
}
else
{
//this for textboxes that will display the data in column..
// I guess that not data contains null 
txtStudName.Text = dt.Rows[0]["StudentName"].ToString() + "";
txtAdd.Text = dt.Rows[0]["StudentAdd"].ToString() + "address";
txtContact.Text = dt.Rows[0]["StudentContact"].ToString() + "Contact"; 
 

 
}


这篇关于如何将数据从MySQL放到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆