我有一些 mysql 问题,我有一个名为“transactionNumber”和“counter”的列。
transactionNumber 值可以是这样的:(不包括字母)
a. 12345, 1234566
b. 9876, 98332, 23423
c. 12347
d. -
'counter' 列是一个字段,用于计算一行中有多少个 transactionNumber。例如上面的字母'a'有2个transactionNumber,字母'b'有3个等等。字母'd'算作1。
但是当应用程序启动时计数器列是空的。所以我创建了一个方法来计算每行的 transactionNUMer 并更新计数器列。
这是我的方法:
private void TransactionNumberCounter(DataSet dsData)
{
for (int i = 0; i < dsData.Tables["data"].Rows.Count; i++)
{
DataRow dRow = dsData.Tables["data"].Rows[i];
string tNumber = dRow.ItemArray.GetValue(1).ToString();
string id = dRow.ItemArray.GetValue(0).ToString();
string[] numbers = tNumbers.Split(',');
char[] arr = new char[] { '\t', '\n' };
int tNumberCounter= 0;
List<string> listAccNumbers = new List<string>();
foreach (string number in numbers)
listAccNumbers.Add(number.TrimStart(arr));
tNumberCounter = listAccNumbers.Count;
string query = @"UPDATE bartran SET aNumberCounter = ? WHERE id = ?;";
OdbcParameter[] parameters = new OdbcParameter[]{
new OdbcParameter("?", aNumberCounter),
new OdbcParameter("?", id)
};
DBServer.ExecuteQuery(query, "database_name", parameters);
}
}
这种方法有效。但是,如果要计算数千行,我会收到错误消息:
Can't connect to MySQL server on "localhost" (10061)"
所以我在这里尝试为你们寻求帮助。我如何计算 t.Number 并根据页面加载计数更新计数器字段。提前致谢!
还有其他可行的方法吗?
EDIT:
这是 DBServer 类:
using System;
using System.Data;
using System.Data.Odbc;
public static class DBServer
{
//Test
private const string Server = "localhost";
private const string Uid = "root";
private const string Pwd = "defaultpass";
//LIVE
//private const string Server = "ipaddress";
//private const string Uid = "root";
//private const string Pwd = "password";
public static DataTable GetTable(string query, string database)
{
using (OdbcConnection cn = new OdbcConnection(
string.Format("Driver={{MySQL ODBC 5.1 Driver}};Server={0};dataBase={1};User={2};Password={3};Option=3;",
Server, database, Uid, Pwd)))
{
using (OdbcCommand cmd = new OdbcCommand(query, cn))
{
DataTable dt = new DataTable();
cn.Open();
using (OdbcDataReader rdr = cmd.ExecuteReader())
{
dt.Load(rdr);
return dt;
}
}
}
}
public static DataTable GetTable(string query, string database, OdbcParameter[] parameters)
{
using (OdbcConnection cn = new OdbcConnection(
string.Format("Driver={{MySQL ODBC 5.1 Driver}};Server={0};database={1};User={2};Password={3};Option=3;",
Server, database, Uid, Pwd)))
{
using (OdbcCommand cmd = new OdbcCommand(query, cn))
{
if (parameters != null)
cmd.Parameters.AddRange(parameters);
DataTable dt = new DataTable();
cn.Open();
using (OdbcDataReader rdr = cmd.ExecuteReader())
{
dt.Load(rdr);
return dt;
}
}
}
}
/// <summary>
/// Executes query to the database (Parameterized)
/// </summary>
/// <param name="query">Query String</param>
/// <param name="database">Target Database</param>
/// <param name="parameters">Collection of parameters</param>
/// <returns>Number of affected records</returns>
public static int ExecuteQuery(string query, string database, OdbcParameter[] parameters)
{
using (OdbcConnection cn = new OdbcConnection( string.Format("Driver={{MySQL ODBC 5.1 Driver}};Server={0};database={1};User={2};Password={3};Option=3;",
Server, database, Uid, Pwd)))
{
using (OdbcCommand cmd = new OdbcCommand(query,cn))
{
if (parameters != null)
cmd.Parameters.AddRange(parameters);
cn.Open();
return cmd.ExecuteNonQuery();
}
}
}
/// <summary>
/// Check if the query has record (Parameterized)
/// </summary>
/// <param name="query">Query string</param>
/// <param name="database">Target database</param>
/// <param name="parameters">Collection of parameters</param>
/// <returns>true if has record else false</returns>
public static bool HasRecord(string query, string database, OdbcParameter[] parameters)
{
using (OdbcConnection cn = new OdbcConnection( string.Format("Driver={{MySQL ODBC 5.1 Driver}};Server={0};database={1};User={2};Password={3};Option=3;",
Server, database, Uid, Pwd)))
{
using (OdbcCommand cmd = new OdbcCommand(query,cn))
{
if (parameters != null)
cmd.Parameters.AddRange(parameters);
cn.Open();
using (OdbcDataReader reader = cmd.ExecuteReader())
{
return reader.HasRows;
}
}
}
}
/// <summary>
/// Check if the query has record
/// </summary>
/// <param name="query">Query string</param>
/// <param name="database">Target database</param>
/// <returns>true if has record else false</returns>
public static bool HasRecord(string query, string database)
{
using (OdbcConnection cn = new OdbcConnection(string.Format("Driver={{MySQL ODBC 5.1 Driver}};Server={0};database={1};User={2};Password={3};Option=3;",
Server, database, Uid, Pwd)))
{
using (OdbcCommand cmd = new OdbcCommand(query, cn))
{
cn.Open();
using (OdbcDataReader reader = cmd.ExecuteReader())
{
return reader.HasRows;
}
}
}
}
public static DataTable CallStoredProcedure(string query, string database, OdbcParameter[] parameters)
{
using (OdbcConnection cn = new OdbcConnection(
string.Format("Driver={{MySQL ODBC 5.1 Driver}};Server={0};database={1};User={2};Password={3};Option=3;",
Server, database, Uid, Pwd)))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand(query, cn))
{
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null)
cmd.Parameters.AddRange(parameters);
OdbcDataReader rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr);
return dt;
}
}
}
}