Link to home
Start Free TrialLog in
Avatar of dodgerfan
dodgerfanFlag for United States of America

asked on

Insert into a SQL server table from a datatable

I'm trying to insert each row from a datatable into a sql server table. I can;t seem to get the syntax right in trying to get it to work. My code is below. I;m not sure how to tell it how to get the values from certain feilds in the datatable.

My code now looks like this:
protected void Import_Click(object sender, EventArgs e)

string strConn = "data source=(local);Inital Catalog="Testing";Integrated Security=true;);
string strSQL = "Select * from view3";
string password = "Password";

Sql Connection my Connection = new SQLConnection(strConn);

myConnection.Open();
SqlDataAdapter da = new SqlDataAdapter(strSQL, myConnection);
DataTable dt = new DataTable();
da.Fill(dt);

foreach (DataRow row in dt.Rows)
{
      MembershipUser newUser = Membership.CreateUser("username", password, "email");
}

Just running the line:  MembershipUser newUser = Membership.CreateUser("username", password, "email"); works in and of itself. What I want to do is run that line for each row in my datatable. The datatable data has two fields for each user, username and email. I'm setting the same password for everyone initially. What is the syntax for using the columns for each row in the datatable instead of the variables in there now?
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dodgerfan

ASKER

Works perfectly. Thanks.