本文介绍了如何在excel导入后将数据插入表中 - 请结束插入的例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string query = "SELECT [USERNAME], [PASSWORD], [FULLNAME] FROM [Users$]";
OleDbCommand cmd = new OleDbCommand(query, oleCn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
grvExcelData.DataSource = dt;
grvExcelData.DataBind();
//saved into the table
string sqlConnectionString = "Data Source=HP-PC\\SQLEXPRESS;Initial Catalog=SAMMY;User ID=benefit";
SqlConnection sqlCon = new SqlConnection(sqlConnectionString);
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlCon;
sqlCmd.CommandType = CommandType.Text;
for (int i = 0; i <= grvExcelData.Rows.Count - 1; i++)
{
string commandText = "INSERT INTO Users(USERNAME, PASSWORD, FULLNAME)" +
"VALUES()";
}
推荐答案
SqlConnection conn;
SqlCommand comm;
SqlDataReader sdr;
string connstring = "Data Source=YourServer\\SQLEXPRESS;Initial Catalog=YourDatabase;Integrated Security=False;MultipleActiveResultSets=true;User Id=YourUsername;Password=YourPassword;Connect Timeout=0";
public void populateUsers()
{
try
{
conn = new SqlConnection(connstring);
conn.Open();
string query = "SELECT SSN, Password, LastName FROM Person";
comm = new SqlCommand(query, conn);
sdr = comm.ExecuteReader();
while(sdr.Read())
{
string sql = "INSERT into Users VALUES (@un, @pw, @fn)";
comm = new SqlCommand(sql, conn);
SqlParameter uname = new SqlParameter("@un", SqlDbType.Text);
uname.Direction = ParameterDirection.Input;
SqlParameter pword = new SqlParameter("@pw", SqlDbType.Text);
pword.Direction = ParameterDirection.Input;
SqlParameter fname = new SqlParameter("@fn", SqlDbType.Text);
fname.Direction = ParameterDirection.Input;
comm.Parameters.Add(uname);
comm.Parameters.Add(pword);
comm.Parameters.Add(fname);
uname.Value = sdr["SSN"].ToString();
pword.Value = sdr["Password"].ToString();
fname.Value = sdr["LastName"].ToString();
comm.ExecuteNonQuery();
}
}
catch(Exception exc)
{
//log or display or ignore error
}
finally
{
sdr.Dispose();
comm.Dispose();
conn.Close();
}
希望有所帮助。
-strimpf
Hope it helps.
-strimpf
这篇关于如何在excel导入后将数据插入表中 - 请结束插入的例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!