I try to point out a few pieces of code that I found inappropriate.
foreach (int num in txtresults.ToString())
textbox.ToString() will get this result:
A string composed of Type and Text property, and you are converting it to int in the foreach loop, so it will be the value corresponding to the current letter in the ASCII table, for example, the first letter S corresponds to 83. I believe this is not you want.
double[] numbers = parts.Select(p => double.Parse(p)).ToArray();
Linq is generally used for filtering and query work, try not to use it for modifying data.
Although it does not produce exceptions, it does not conform to general coding standards and may confuse people at some point. It is better to use general loops.
double[] numbers = parts.Select(p => double.Parse(p)).ToArray();
double maximum = numbers.Max();
int position = Array.IndexOf(numbers, maximum);
I don't understand why we need to find the position, using parts[position] will always only add th after the largest number, no matter how many times it loops.
I made some changes to the code, the current code is as follows:
private void button1_Click(object sender, EventArgs e)
{
string[] parts = textBox1.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
double[] numbers = new double[parts.Length];
for (int i = 0; i < parts.Length; i++)
{
numbers[i] = double.Parse(parts[i]);
}
double[] sortedNums = numbers.OrderByDescending(n => n).ToArray();
int[] positions = new int[numbers.Length];
for (int i = 0; i < positions.Length; i++)
{
positions[i] = Array.IndexOf(sortedNums, numbers[i])+1;
}
for (int i = 0; i < numbers.Length; i++)
{
parts[i] = AddOrdinal(positions[i]);
}
textBox1.Text += Environment.NewLine;
textBox1.Text += string.Join(" ", parts);
DataTable dataTable= ToDataTable(parts, numbers);
InsertIntoDataBase(dataTable);
}
public static void InsertIntoDataBase (DataTable dataTable)
{
string connString = @"your connection string";
using (SqlConnection sqlConnection = new SqlConnection(connString))
{
sqlConnection.Open();
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlConnection))
{
sqlBulkCopy.DestinationTableName =
"dbo.rangkingTable";
sqlBulkCopy.WriteToServer(dataTable);
}
}
}
public static DataTable ToDataTable(string[] ranks,double[] scores)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("score", typeof(double));
dataTable.Columns.Add("rank", typeof(string));
for (int i = 0; i < ranks.Length; i++)
{
dataTable.Rows.Add(i + 1, scores[i], ranks[i]);
}
return dataTable;
}
Result:
Is this what you need?
If not, please describe what the final result you need is.
Update(2021.3.10):
To create a new table as an example.
First, create a new table in the database.
CREATE TABLE [dbo].[rangkingTable]
(
[Id] INT NOT NULL PRIMARY KEY,
[score] FLOAT NULL,
[rank] NCHAR(10) NULL
)
Then refer to the two new methods I added in the code above.
Update(2021.3.11):
Take the rankingTable I created yesterday as an example, suppose it looks like this now:
We can update it in batches through SqlDataAdapter, like this:
public static void UpdateTable(string[] ranks)
{
string connString = @"your conn string";
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
SqlDataAdapter dataAdpater = new SqlDataAdapter(
"select * from rankingTable",
connection);
dataAdpater.UpdateCommand = new SqlCommand(
"UPDATE rankingTable SET rank = @rank " +
"WHERE id = @id", connection);
dataAdpater.UpdateCommand.Parameters.Add(
"@rank", SqlDbType.NVarChar, 15, "rank");
SqlParameter parameter = dataAdpater.UpdateCommand.Parameters.Add(
"@id", SqlDbType.Int);
parameter.SourceColumn = "id";
parameter.SourceVersion = DataRowVersion.Original;
DataTable dataTable = new DataTable();
dataAdpater.Fill(dataTable);
for (int i = 0; i < dataTable.Rows.Count; i++)
{
dataTable.Rows[i]["rank"] = ranks[i];
}
dataAdpater.Update(dataTable);
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.