Helllo, mySQL 데이터베이스가 있는데 SQL select 쿼리를 통해 정보를 가져 와서이 정보를 DataGride보기에 표시하려고합니다. 이미 데이터베이스 클래스를 작성했습니다.mysql 데이터베이스에서 datagridview 채우기 C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using MySql;
using MySql.Data;
using System.Data;
using Renci.SshNet;
using Renci.SshNet.Common;
using System.Windows.Forms;
using System.Net.NetworkInformation;
namespace ExpTrackNEA
{
class DatabaseManager
{
private MySqlConnection _Conn;
public MySqlCommand Cmd;
private MySqlDataAdapter _da;
private DataTable _dt;
private MySqlDataReader _dr;
// This is the Database connection function
public bool DBConnection()
{
// First step is to create an SSH Tunnel. This is done by calling the SSHTunnelCreate function
if (SSHTunnelCreate() == true)
{
try // Start of Database connection Try attempt
{
// Define the connection cerdentials
string ConnectionString = null;
ConnectionString = "server=SERVER;" + // Database Address
" port=PORT;" + // Port
" database=DATABSAE;" + // Database Name
" uid=USERNAME;" + // Username
" pwd=PASSWORd;"; // Password
// Define the connection
_Conn = new MySqlConnection(ConnectionString);
try // Attempt a connection
{
// Open the connection
_Conn.Open();
// Return true if connection is successful to the database
return true;
} // Rnd of database connection attempt
catch // Catach Database connection errors
{
// Return False if a connection to the Database can't be made
return false;
} // End of Database connection errors catach
} // End of Database connection Try attempt
catch
{
// If the connection is not successful it returns FALSE as it can't connect
// to the database and the whole process is halted
return false;
} // End of catch for Database Connection
}// End of (SSHTunnelCreate() == true)
else // If the SSH Tunnel Connection Failed
{
return false;
}
} // End of DBConnection Function
private bool SSHTunnelCreate()
{
// Declaring the Connection string information
ConnectionInfo ConnectionInformation = new ConnectionInfo(
"ssh.payneslan.co.uk", // Host Name
22, // Connection Port
"UserNAME", // Username
new AuthenticationMethod[]{ // Define the Password
// Pasword based Authentication
// Define the connection information Username and Password respectfullt
new PasswordAuthenticationMethod("USERNAME","PASSWORD")
}
); // End of ConnectionInformation
using (var client = new SshClient(ConnectionInformation))
{
// Start an attempt to build an SSH Tunnel
try
{ // SSH Tunnel Try Start
client.Connect();
if (client.IsConnected)
{
try
{
var PortFwdL = new ForwardedPortLocal("127.0.0.1", 3306, "localhost", 3306);
//ForwardedPortLocal PortFwdL = new ForwardedPortLocal("127.0.0.1", Convert.ToUInt32(22), "127.0.0.1", Convert.ToUInt32(3306));
client.AddForwardedPort(PortFwdL);
PortFwdL.Start();
// Checking Port Forwarding is working
if (PortFwdL.IsStarted)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
} // End of IF
else
{
// If the connection is not successful it returns FALSE as it now doesn't
// have a secure connection to the server and now the database connection will
// halt.
return false;
} // End of Else
} // End of Try
catch // connection catch for SSH Tunnel creation
{
// If the connection is not successful it returns FALSE as it now has a
// secure connection to the server and now the database connection will
// halt
return false;
} // End of connection catch for SSH Tunnel creation
} // End of using (var client = new SshClient(ConnectionInformation))
} // End of SSHTunnelCreate Function.
public void SQLQuery(string QueryText)
{
Cmd = new MySqlCommand(QueryText, _Conn);
}
public string strSQLQuery(string QueryText)
{
Cmd = new MySqlCommand(QueryText, _Conn);
MySqlCommand cmd = new MySqlCommand(QueryText, _Conn);
_dr = cmd.ExecuteReader();
string _value = null;
while (_dr.Read())
{
_value = _dr.GetString(0);
}
_dr.Close();
return _value;
}
// For The select queries
public DataGrid QueryEx(string QueryText)
{
MySqlDataAdapter adp = new MySqlDataAdapter("Select * from table1;", _Conn);
DataSet ds = new DataSet();
adp.Fill(ds);
//change name according to your datagridview
DataGrid dataGridView1 = new DataGrid();
dataGridView1.DataSource = ds;
// dataGridView1.DataBind();
return dataGridView1;
}
// For Insert,Update and Delete etc.
public void NonQueryEx()
{
try
{
Cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("An error occured when trying to perform this databse action" + "\n" +
"Your action was not completed.",
"Error executing Action",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
} // End Of Class DatabaseManager
}
그리고 내가하고 싶은 무엇을이 방법의 queryex에 SQL 쿼리를 전달하고 모든 필드를 얻기 위해이 방법을 얻고 그 양식에 단순히 뭔가를 넣을 수 있도록 상태에서 그것을 돌려 이.
DGV_Users.DataSource = DataBase.QueryEx("SELECT * FROM
사용자 ");
나는 시도하고 며칠 동안이 작업을 수행하는 데 실패하고 수많은 비디오를 본하지만 난 어떤 일이 동작하지 않습니다 수 있습니다.
감사합니다.
해당 코드의 문제점은 무엇입니까? –
채우기 : SelectedCommand.connectionproperty가 초기화되지 않았습니다. " –
데이터베이스"헬퍼 "클래스를 만드는 것은 유감입니다. [ask]를 읽고 [둘러보기] – Plutonix