通过EXCEL导入数据到grideView的,再绑定dataset,就差写最后一段代码,把dataset的数据更新到数据库不会写,请问如何继续写下去?高手帮忙解决一下~~~不胜感激~~
解决方案 »
- webBrowser跨域frame
- 求一下算法,两表比对问题
- 打包工具tiger的使用(图标问题)
- toolStripStatusLabel2.Text = "ddd"; 显示不出ddd
- 达人们,C#控件,如何实现form的这种式样?
- 请问C#中有多少种情况需要显示转换?
- 读取Excel中Sheet名称时出现乱码?(高分,解决立刻结帖)
- windows media player 9控件能否调用EQ(均衡?
- 请问哪位用.net做过目录权限管理的?
- 在vs C#里,有没有类似于vb6.0那样所带的winapi viewer
- 紧急求救一个关于OpenFileDialog类的问题
- 写入二进制文件时的问题
Dim cmm As New SqlCommand("select * from 表名", con)
Dim adp As New SqlDataAdapter()
adp.SelectCommand = cmm
Dim cb As New SqlCommandBuilder(adp)
Try
con.Open() adp.Update(dataset.tables(0))
Catch ex As Exception
MsgBox(ex.Message)
End Try
这是从dataGridView1中读的,你可以根据条件来读。 for (iRows = 0; iRows < intCount; iRows++)
{
progressBar1.Value = iRows;
sw[0] = "'" + dataGridView1.Rows[iRows].Cells[0].Value.ToString() + "'";
sw[1] = "'" + dataGridView1.Rows[iRows].Cells[1].Value.ToString() + "'";
sw[2] = "'" + dataGridView1.Rows[iRows].Cells[2].Value.ToString() + "'";
sw[3] = "'" + dataGridView1.Rows[iRows].Cells[3].Value.ToString() + "'";
sw[4] = "'" + dataGridView1.Rows[iRows].Cells[4].Value.ToString() + "'";
sw[5] = "'" + dataGridView1.Rows[iRows].Cells[5].Value.ToString() + "'";
sw[6] = "'" + dataGridView1.Rows[iRows].Cells[6].Value.ToString() + "'";
sw[7] = "'" + dataGridView1.Rows[iRows].Cells[7].Value.ToString() + "'";
sw[8] = "'" + dataGridView1.Rows[iRows].Cells[8].Value.ToString() + "'";
sw[9] = "'" + dataGridView1.Rows[iRows].Cells[9].Value.ToString() + "'";
SqlConnection con=new SqlConnection(SourceConnection);
SqlCommand cmm=New SqlCommand("select 字段1,字段2...字段N from 表名", con);
SqlDataAdapter adp=new SqlDataAdapter();
adp.SelectCommand = cmm;
SqlCommandBuilder cb=new SqlCommandBuilder(adp);try{
con.Open();
adp.Update(dtb);
}
//以下代码略.
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["schoolConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
private DataSet CreateDataSource()
{
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("" + DropDownList1.Text + "" + TextBox1.Text + "") + "; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection olecon = new OleDbConnection(strCon); OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon); DataSet myds = new DataSet();
myda.Fill(myds); return myds;
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}我原程序是这样的,从EXCEL导入到GridView,再绑定DATASET,但是后面的写入SQL,就不会了。
foreach (DataRow row in table.Rows)
{
sqlstr = "insert into T_info (xm,xb,bz) values('" + row[0].ToString() + "','" + row[1].ToString() + "','" + row[2].ToString() + "')";
mycomm = new OleDbCommand(sqlstr, mydbconn);
mycomm.ExecuteNonQuery();
}这里是循环到 row ,
你想要列的话,foreach(DataColumn column in table.Columns){}
{
string connectstring = "...";
string sqlstr=string.Empty;
DataSet myds = CreateDataSource();
GridView1.DataSource = myds;
GridView1.DataBind();
SqlConnection myConn =new SqlConnection(connectstring);
myConn.open();
SqlCommand myComm;
foreach (DataTable table in myds.Tables)
foreach (DataRow row in table.Rows)
{
sqlstr = "insert into T_info (xm,xb,bz) values('" + row[0].ToString() + "','" + row[1].ToString() + "','" + row[2].ToString() + "')";
//这里改成你自己表的格式
mycomm = new OleDbCommand(sqlstr, mydbconn);
mycomm.ExecuteNonQuery();
}
myConn.close();
}
//这里改成你自己表的格式
mycomm = new OleDbCommand(sqlstr, mydbconn);
mycomm.ExecuteNonQuery();
错误 1 当前上下文中不存在名称“mycomm”
错误 2 当前上下文中不存在名称“mydbconn”
错误 3 当前上下文中不存在名称“mycomm”
请问怎样解决呢??
{
string connectstring = "...";
string sqlstr=string.Empty;
DataSet myds = CreateDataSource();
GridView1.DataSource = myds;
GridView1.DataBind();
SqlConnection myConn =new SqlConnection(connectstring);
myConn.open();
SqlCommand myComm;
foreach (DataTable table in myds.Tables)
foreach (DataRow row in table.Rows)
{
sqlstr = "insert into T_info (xm,xb,bz) values('" + row[0].ToString() + "','" + row[1].ToString() + "','" + row[2].ToString() + "')";
//这里改成你自己表的格式
myComm = new SqlCommand(sqlstr, myConn);
myComm.ExecuteNonQuery();
}
myConn.close();
}