C# Can';t从表中获取所需的记录
我做了一个函数,可以为数据库中的记录生成一个新ID 它只需获取上一条记录的ID,然后将其向前移动一个字母即可。例如,如果前一个ID是AAA,则新ID将是AAAB,依此类推 获取上一个ID的方法是计算表中的行数,然后使用该数字获取表中的最后一条记录。我之所以去掉1,是因为行计数器没有索引为0,但记录数为。因此,在一个有50条记录的表中,最后一条记录将记录49条 问题是此函数仅对1条记录有效。因此,只有第一个生成的ID将向前移动,其余ID将与第二个ID完全相同。例如,这就是它的外观 0-AAA, 1–AAB, 2–AAB, 3–AABC# Can';t从表中获取所需的记录,c#,sql,database,oop,C#,Sql,Database,Oop,我做了一个函数,可以为数据库中的记录生成一个新ID 它只需获取上一条记录的ID,然后将其向前移动一个字母即可。例如,如果前一个ID是AAA,则新ID将是AAAB,依此类推 获取上一个ID的方法是计算表中的行数,然后使用该数字获取表中的最后一条记录。我之所以去掉1,是因为行计数器没有索引为0,但记录数为。因此,在一个有50条记录的表中,最后一条记录将记录49条 问题是此函数仅对1条记录有效。因此,只有第一个生成的ID将向前移动,其余ID将与第二个ID完全相同。例如,这就是它的外观 0-AAA, 1
// Generate codes in order
public string StrGenerateSequentialCode(string StrTableName)
{
// Get the places for the counters from the previous code
AccessTable ObjStoreCode = new AccessTable();
ObjStoreCode.SelectTable(StrTableName);
string StrPreviousID = "";
StrPreviousID = ObjStoreCode.StrGetRow(ObjStoreCode.IntGetRowCount() - 1, 0);
下面是该函数的其余代码
char[] ArrCollection = new char[36] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int[] ArrPreviousIDSpace = new int[11]; // the number if chars for this code is fixed to 11
for (int i = 0; i < ArrPreviousIDSpace.Length; i++)
{
for (int j = 0; j < ArrCollection.Length; j++)
{
if (ArrCollection[j] != StrPreviousID[i])
{
ArrPreviousIDSpace[i]++;
}
else
{
break;
}
}
}
// Now generate a code with each character carrying on from the last
string StrCode = "";
/* Add one to the last parts position until it reaches 27,
* when it does set its position to 0 and then add one to the second last parts
position and repeat the process for the third last part...*/
int A = 0, B = 0, C = 0, D = 0, E = 0, F = 0, G = 0, H = 0, I = 0, J = 0, K = 0;
// Make the starting points for the char selecters the
A = ArrPreviousIDSpace[0];
B = ArrPreviousIDSpace[1];
C = ArrPreviousIDSpace[2];
D = ArrPreviousIDSpace[3];
E = ArrPreviousIDSpace[4];
F = ArrPreviousIDSpace[5];
G = ArrPreviousIDSpace[6];
H = ArrPreviousIDSpace[7];
I = ArrPreviousIDSpace[8];
J = ArrPreviousIDSpace[9];
K = ArrPreviousIDSpace[10];
// Turn the clock
K++;
if (K == ArrCollection.Length)
{
K = 0;
J++;
}
if (J == ArrCollection.Length)
{
J = 0;
I++;
}
if (I == ArrCollection.Length)
{
I = 0;
H++;
}
if (H == ArrCollection.Length)
{
H = 0;
G++;
}
if (G == ArrCollection.Length)
{
G = 0;
F++;
}
if (F == ArrCollection.Length)
{
F = 0;
E++;
}
if (E == ArrCollection.Length)
{
E = 0;
D++;
}
if (D == ArrCollection.Length)
{
D = 0;
C++;
}
if (C == ArrCollection.Length)
{
C = 0;
B++;
}
if (B == ArrCollection.Length)
{
B = 0;
A++;
}
// Combine the chars to make a final password
StrCode = ArrCollection[A].ToString() + ArrCollection[B].ToString() + ArrCollection[C].ToString() + ArrCollection[D].ToString() + ArrCollection[E].ToString() + ArrCollection[F].ToString() + ArrCollection[G].ToString() + ArrCollection[H].ToString() + ArrCollection[I].ToString() + ArrCollection[J].ToString() + ArrCollection[K].ToString();
return StrCode;
char[]ArrCollection=新字符[36]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9';
int[]arrprovioidspace=new int[11];//此代码的字符数if固定为11
for(int i=0;i
下面是从表中获取记录的函数
public string StrGetRow(int IntSelectedRow = 0, int IntSelectedColumn = 0)
{
string StrRequesedRow = "";
// This if statement will check whether or not the selected coloumns or rows are larger than the amount available
if (IntSelectedRow < ObjDataSet.Tables[0].Rows.Count & IntSelectedColumn < ObjDataSet.Tables[0].Columns.Count)
{
// Make the table the data row origianates from the table on the dataset
DataRow = ObjDataSet.Tables[0].Rows[IntSelectedRow];
// This will store the data in the string 'StrRequestedRow
StrRequesedRow = DataRow.ItemArray.GetValue(IntSelectedColumn).ToString();
}
else
{
StrRequesedRow = "NO MORE RECORDS";
}
return StrRequesedRow;
}
公共字符串StrGetRow(int-IntSelectedRow=0,int-IntSelectedColumn=0)
{
字符串strRequestRow=“”;
//此if语句将检查所选列或行是否大于可用数量
if(IntSelectedRow
如果我记得我的模块演算,你的算法应该是这样的
public static string NumberToString(int number, int length,char[] allChars)
{
var remain = number;
var result = "";
var total = allChars.Length;
while (remain >= total)
{
result = allChars[remain % total]+result;
remain = (int)Math.Floor(remain * 1.0 / total);
}
result = allChars[remain]+result;
return result.PadLeft(length, allChars[0]);
}
这可以优化,但很简单。你是这样用的
static void Main(string[] args)
{
char[] allChars = { 'A', 'B', 'C', 'D' };
var count = 0;
Console.WriteLine(NumberToString(count,3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(++count, 3, allChars));
Console.WriteLine(NumberToString(30, 3, allChars));
Console.WriteLine(NumberToString(63, 3, allChars));
Console.WriteLine("Done");
Console.ReadLine();
}
只需使用行数并使用任意长度和字符集即可。
但不要说我没有警告你并发性。您必须在读取行数期间锁定表,直到插入新行为止。因为如果有人在这段时间内插入一个新行,您将得到ID冲突。您这样做完全错误。每个现代DBMS都有一种插入记录然后返回新记录ID的方法。您使用的是什么数据库管理系统?我将Microsoft SQL与OLEDB结合使用。另外,我刚刚添加了从表中获取记录的函数,以便更好地描述。整个过程有点愚蠢。如果您真的不想使用自动生成的id来生成新id或其他不依赖于数据库的内容。由于多种原因(并发性、性能、锁…),您的方式有点不好。我之所以这样做,是因为我希望它采用特定的格式。谢谢。你是一个sa