Search code examples
c#databasevisual-studio-2005variablesoledb

Use of unassigned local variable 'xxx'


I'm writing a database importer from our competitors to ours database:) I have a code generator which create Methods form import to our database like

public void Test_Import_Customer_1()

 // variables
 string conn;
 string sqlSelect;
 string sqlInsert;

 int extID;
 string name;
 string name2;
 DateTime date_inserted;

 sqlSelect="select id,name,date_inserted from table_competitors_1";
 oledbreader reader = new GetOledbRader(sqlString,conn);
 while (reader.read())
 {
  name=left((string)myreader["name"],50); //limitation of my field
  date_inserted=myreader["date_inserted"];
  sqlInsert=string.Format("insert into table(name,name2,date_inserted)values '{0}', '{1}', {2})",name,name2,date_inserted); //here is the problem name2 "Use of unassigned local variable"
  ExecuteSQL(sqlInsert)
 }

As different companies database has different fields i can not set value to each variable and there is a big number of tables to go one variable to next.

like

sqlSelect_Company_1 = "select name,date_inserted from table_1";
sqlSelect_Company_2 = "select name,name2 from table_2";

is there a way to override the typing of each variable one by one with default values?


Solution

  • Rather than storing your parameter values in variables store them in a Dictionary object with the keys as your field names and your values/params as the values.

    Create a generic method that takes a string for the tablename and the dictionary for the params. You can build up your sql string using a string builder.