-1
つまり、私は
example[0][1]
を呼び出すことによって、MySQLデータベース用の
List<string>[]
を返す関数を作っていると私は私が取得するためにwan't列を指定する必要が
dbConnect = new DBConnect();
List<string>[] userlogin;
List<MySqlParameter> parameters = new List<MySqlParameter>();
parameters.Add(new MySqlParameter("@Username", usernameinput.Text));
parameters.Add(new MySqlParameter("@Keya", passwordhashed));
dbConnect.Select("SELECT `id`, `Username`, `Key`, `AdminLevel`,`Model` FROM `accounts` WHERE `Username` = @Username AND `Key` = @Keya", parameters, 5, "Username", "AdminLevel", "Key", "id", "Model");
私のクラスの取得
dbConnect.Select("SELECT * FROMすなわちaccounts
WHEREUsername
= @Username ANDKey
= @Keya", parameters);
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System;
namespace something
{
class DBConnect
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
public bool connectionstatus = false;
//Constructor
public DBConnect()
{
Initialize();
}
//Initialize values
private void Initialize()
{
server = "127.0.0.1";
database = "smth";
uid = "root";
password = "123123";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
this.OpenConnection();
}
//open connection to database
public bool OpenConnection()
{
if (connectionstatus != true)
{
try
{
connection.Open();
connectionstatus = true;
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
return true;
}
//Close connection
public bool CloseConnection()
{
if (connectionstatus == true)
{
try
{
connection.Close();
connectionstatus = false;
return true;
}
catch (MySqlException ex)
{
ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show(ex.Message);
//MessageBox.Show(ex.Message);
return false;
}
}
return true;
}
public List<string>[] Select(string query, List<MySqlParameter> Values, int numofcolumns, params string[] columns)
{
string[] columns2 = columns;
//numofcolumns -= 1;
for (int i = 0; i < numofcolumns; i++)
columns2[i] = string.Format("`{0,-3}`", columns[i]);
string columns3 = string.Join(",", columns2);
columns3 = Functions.TrimAllWithInplaceCharArray(columns3);
//string query = "SELECT "+ columns3 + " FROM `"+ table + "` WHERE " + whereclause;
//Create a list to store the result
List<string>[] list = new List<string>[numofcolumns];
for (int i = 0; i < numofcolumns; i++)
{
list[i] = new List<string>();
}
//Open connection
if (connectionstatus == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
foreach (MySqlParameter param in Values)
{
cmd.Parameters.Add(param);
}
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
for (int i = 0; i < numofcolumns; i++)
{
columns[i] = columns[i].Trim(new Char[] { ' ', '`' });
list[i].Add(dataReader[columns[i].ToString()].ToString());
}
}
//close Data Reader
dataReader.Close();
//close Connection
//this.CloseConnection();
//return list to be displayed
return list;
}
else
{
return list;
}
}
}
}
しかし、私はwan'tちょっと便利な..ですつまり、私はexample[0]["table"]
を言って/コールを使用することができますし、私はテーブルを指定する必要はありません...
私は約24時間、またはいくつかのために、いくつかの回避策の後に私の質問を解決してきた...
作業を...
誰もがなぜ私の質問に答えるのは有用ではないのですか? –