I am trying to create a windows form with Visual Studio 2012, and working with an MS Access database. This form will programmatically create a database and manually input all data into the fields bases on user input. I can create the database and tables just fine, the primary keys also auto-increment. What I am trying to accomplish is I would like to input a beginning and ending number, say a year for example, and then loop through the range of numbers to input redundant data and the incremented year that would save a ton of typing. I have the loop working and the data inputs into each record through the length of the loop, but the number I need to increment does not increment. How can I get a second field to increment based on user input when there is already an auto-increment primary key? Any insight would be greatly appreciated, my problem method is below.
private void button3_Click(object sender, EventArgs e)
{
char ch = '"';
string sql = "";
string tempYear = "";
int year = Convert.ToInt32(this.textBoxBegYear.Text);
int endYear = Convert.ToInt32(this.textBoxEndYear.Text);
try
{
OleDbConnection myConnection = new OleDbConnection();
myConnection.ConnectionString
"Provider=Microsoft.ACE.OLEDB.12.0DataSource=" + ch + openFileDialog1.FileName + ch;
myConnection.Open();
sql = "INSERT INTO " + this.comboBox1.SelectedItem.ToString() + " (Year) Values (@tempYear)";
OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
for (int i = year; i <= endYear; i++)
{
tempYear = Convert.ToString(i);
myCommand.Parameters.Add("@tempYear", OleDbType.Numeric).Value = tempYear;
myCommand.ExecuteNonQuery();
}
myCommand.Connection.Close();
}
catch (Exception err)
{
MessageBox.Show("Error: " + err.Message.ToString());
}
}
I would like the finished output to be as follows but I need to get the number to increment first, the rest works fine...
Year ...all other fields
2000 ...stuff
2001 ...identical stuff from previous record
2002 ...identical stuff from first record
Thank You.