Reputation: 1
i am trying to insert to insert below data as my requirement. ex: my excel sheet like this:
id name codes
1 a 12
2 b 13,14,15
3 c 16-19
my requirement like this:
id name codes
1 a 12
2 b 13
2 b 14
2 b 15
3 c 16
3 c 17
3 c 18
3 c 19
am using c#. can anyone help me.... thnx in advance.
my code:
for (rCnt = 2; rCnt <= range.Rows.Count; rCnt++)
{
cmd = "insert into " + tablename + " values (";
effective_date = VerifyDateTime(range.Cells[rCnt, 7].Value);
destination = (string)(range.Cells[rCnt, 1] as Excel.Range).Value2;
prefix = range.Cells[rCnt, 3].Value.ToString();
codes = range.Cells[rCnt, 2].Value.ToString();
level = range.Cells[rCnt, 5].Value.ToString();
rate = range.Cells[rCnt, 4].Value.ToString();
change = range.Cells[rCnt, 6].Value.ToString();
company_id = cmbcompanyid.SelectedItem.ToString();
string s = range.Cells[rCnt, 6].Value.ToString();
List<string> l = new List<string>(s.Split(';', '-'));
int le = 0;
for (le = 0; le <= l.Count; le++)
{
change = l[le];
cmd = cmd + "'" + destination + "','" + codes + "','" + prefix + "','" + rate + "','" + level + "','" + change + "','" + effective_date + "','" + company_id + "')";
// cmd = cmd + "'" + destination + "','" + codes + "','" + prefix + "','" + rate + "','" + level + "','" + change + "','" + effective_date + "','" + company_id + "')";
cmd = ReplaceSpecialCharacters(cmd);
MySqlCommand sqlCmd = new MySqlCommand(cmd, sqlCon);
var i = sqlCmd.ExecuteNonQuery();
}
}
Upvotes: 0
Views: 796
Reputation: 39329
There is no built-in way to do this, but it's fairly easy to do yourself:
,
to get separate values-
to get the range limits. One result = no range; two results = lower and upper limit, so emit all numbers inbetween.The main method to use here is .Split().
The full implementation is left as an exercise to the reader :-)
EDIT
Note that you turn one original value into potentially a lot of new values. A single 'insert' is not enough to process one input line.
Upvotes: 1
Reputation: 700562
You would have to parse the string, something like this:
string[] items = value.Split(",");
foreach (string item in items) {
if (item.Contains("-")) {
string[] parts = item.Split("-");
int min = Int32.Parse(parts[0]);
int max = Int32.Parse(parts[1]);
for (int i = min; i <= max; i++) {
// add the value in i to the data
}
} else {
// add the value in item to the data
}
}
Upvotes: 0