Hey all,
having a little trouble transferring my listview items into a database. I have transfered all of the listview items into a List<> for use during the population of the items in my database
here is my dataaccessor class, it contains the reader writer and writer statements generator:
public class SQLDBDataAccessorContactOrganizer : IContactsOrganizerDataAccessor
{
private const string CONNECTION_STRING = @"Data Source=SERVER\INSTANCE;Initial Catalog=ContactList;User Id=SA;Password=SQL;";
private const string SELECT_STATEMENT = @"SELECT * FROM ContactList";
public void writeContactList(List<Contact> contactListing)
{
List<Contact> organizerContacts = new List<Contact>();
SqlConnection connectionToContactDB = new SqlConnection(CONNECTION_STRING);
connectionToContactDB.Open();
SqlCommand commandLine;
string[] statementsContainer = statementCreator(contactListing);
for (int i = 0; i < statementsContainer.Length; i ++)
{
commandLine = new SqlCommand(statementsContainer[i], connectionToContactDB);
}
connectionToContactDB.Close();
}
public List<Contact> readContactList()
{
List<Contact> organizerContacts = new List<Contact>();
SqlConnection connectionToContactDB = new SqlConnection(CONNECTION_STRING);
connectionToContactDB.Open();
SqlDataAdapter contactDataAccessor = new SqlDataAdapter(SELECT_STATEMENT, connectionToContactDB);
DataSet contactDataSet = new DataSet();
contactDataAccessor.Fill(contactDataSet);
foreach (DataRow contactRow in contactDataSet.Tables[0].Rows)
{
Contact currentContactRecord = new Contact();
currentContactRecord.lastNameSearchAssign(contactRow["Full Name"].ToString());
currentContactRecord.firstNameSearchAssign(contactRow["Full Name"].ToString());
currentContactRecord.middleNameSearchAssign(contactRow["Full Name"].ToString());
currentContactRecord.CompanyName = contactRow["Company Name"].ToString();
currentContactRecord.JobTitle = contactRow["Job Title"].ToString();
currentContactRecord.HomeAddress = contactRow["Home Address"].ToString();
currentContactRecord.EmailAddress = contactRow["E - Mail Address"].ToString();
currentContactRecord.MessengerAddress = contactRow["IM Address"].ToString();
currentContactRecord.BusinessPhoneNumber = Convert.ToInt32(contactRow["Business Phone Number"].ToString());
currentContactRecord.HomePhoneNumber = Convert.ToInt32(contactRow["Home Phone Number"].ToString());
currentContactRecord.FaxPhoneNumber = Convert.ToInt32(contactRow["Fax Phone Number"].ToString());
currentContactRecord.MobilePhoneNumber = Convert.ToInt32(contactRow["Mobile Phone Number"].ToString());
organizerContacts.Add(currentContactRecord);
}
connectionToContactDB.Close();
return organizerContacts;
}
private string[] statementCreator(List<Contact> contactListing)
{
string[] sqlStatementContainer = new string [contactListing.Count];
for (int i = 0; i < sqlStatementContainer.Length; i++)
{
sqlStatementContainer[i] = "INSERT INTO ContactList VALUES ('" +
contactListing[i].LastName + "', '" +
contactListing[i].FirstName + "', '" +
contactListing[i].MiddleName + "', '" +
contactListing[i].CompanyName + "', '" +
contactListing[i].JobTitle + "', '" +
contactListing[i].HomeAddress + "', '" +
contactListing[i].EmailAddress + "', '" +
contactListing[i].MessengerAddress + "', '" +
contactListing[i].BusinessPhoneNumber + "', '" +
contactListing[i].HomePhoneNumber + "', '" +
contactListing[i].FaxPhoneNumber + "', '" +
contactListing[i].MobilePhoneNumber + "')";
}
return sqlStatementContainer;
}
I believe I having quite a problem with my writer function. This is my first time to use SQL with C# and have no idea what I am doing wrong. My reader function was just derived from a code that I saw in the internet about reading files from a database into a class and then into a list. Please show me how do I connect and write my data onto the database properly.
THANKYOU!