看怎么连接了。如果是横向的,就是 join
纵向的,就是投影运算,比如 union。
纵向的,就是投影运算,比如 union。
解决方案 »
- 急!!!!求高手,treeview的动态绑定问题
- 关于c#中SqlDataReader的GetString()方法的疑惑.
- timer重复执行问题
- 半夜1点求急救··关于C#的c/s编码问题
- #标题党#请问为什么VS2003编译的程序能连通某个硬件设备VS2005编译后就不行了呢?
- c#成员变量跟static的关系(在线)
- C#做的winform程序,怎样让他出错之后能自己结束所有线程?
- C#中"你要在連接字串中包含密碼嗎?"是什麼意思,能告訴我嗎?
- 为什么datagrid和文本框中的内容不能同步更新?
- 请大虾们看看这段程序有什么问题
- [使用OpenXML SDK操作Excel报表]单元格百分比的问题
- 急急急
不是好方法,好方法没想到
DataSet ds = new DataSet();
SqlDataAdapter sda;SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds);
this.dataGridView1.DataSource = ds.Tables[0];或OleDbDataAdapter更新数据库
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";" +
"Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
DataSet ds= new DataSet();
using(OleDbConnection myConn = new OleDbConnection(strConn))
{
myConn.Open();
string str = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(str, myConn);
myCommand.Fill(ds);
myConn.Close();
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Data.OleDb;
using System.Data.Common;namespace lv
{
public partial class zsxx : Form
{
public zsxx()
{
InitializeComponent();
} private void zsxx_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“ymDataSet9.证书获取表”中。您可以根据需要移动或移除它。 }
//internal void open()
//{ //} string strSource;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
string conString = file.FileName;
string strSource = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + conString + "; Extended Properties ='Excel 8.0;HDR=No'";
OleDbConnection conn = new OleDbConnection(strSource);
string sqlstring = @"SELECT * FROM [Sheet1$]";
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlstring, conn);
DataSet da = new DataSet();
adapter.Fill(da);
this.dataGridView1.DataSource = da.Tables[0];
dataGridView1.Columns[0].HeaderCell.Value = "Xuehao";
dataGridView1.Columns[1].HeaderCell.Value = "Xingming";
dataGridView1.Columns[2].HeaderCell.Value = "Xingbie";
dataGridView1.Columns[3].HeaderCell.Value = "Xibie";
dataGridView1.Columns[4].HeaderCell.Value = "Zhuanye";
dataGridView1.Columns[5].HeaderCell.Value = "Banbie";
dataGridView1.Columns[6].HeaderCell.Value = "Xuezhi";
dataGridView1.Columns[7].HeaderCell.Value = "Zhuce";
dataGridView1.Columns[8].HeaderCell.Value = "Ruxue";
dataGridView1.Columns[9].HeaderCell.Value = "Sfzh";
}
} private void button2_Click(object sender, EventArgs e)
{
try
{
if (dataGridView1.Rows.Count > 0)
{ for (int i = 0; i < dataGridView1.Rows.Count; i++)
{ string strSql = " ";
SqlCommand myCmd = null;
string xh = "";
string xm = "";
string xb = "";
string zy = "";
string nj = "";
string bb = "";
string sfzh = "";
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{ if (dataGridView1.Columns[j].HeaderCell.Value.ToString() == "Xuehao")
{ xh = dataGridView1.Rows[i].Cells[j].Value.ToString();
} if (dataGridView1.Columns[j].HeaderCell.Value.ToString() == "Xingming")
{
xm = dataGridView1.Rows[i].Cells[j].Value.ToString();
} if (dataGridView1.Columns[j].HeaderCell.Value.ToString() == "Xibie")
{
xb = dataGridView1.Rows[i].Cells[j].Value.ToString(); } if (dataGridView1.Columns[j].HeaderCell.Value.ToString() == "Zhuanye")
{
zy = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
if (dataGridView1.Columns[j].HeaderCell.Value.ToString() == "Xuezhi")
{
nj = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
if (dataGridView1.Columns[j].HeaderCell.Value.ToString() == "Banbie")
{
bb = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
if (dataGridView1.Columns[j].HeaderCell.Value.ToString() == "Sfzh")
{
sfzh = dataGridView1.Rows[i].Cells[j].Value.ToString();
} }
strSql = "insert into 学生资料表(学号,姓名,系别,专业,年级,班别,身份证号) values ( '" + xh + "','" + xm + "','" + xb + "','" + zy + "','" + nj + "','" + bb + "','" + sfzh + "')";
string strConnection = "Data Source=.;Initial Catalog=lx;Integrated Security=True"; //连接数据库的连接字符串(仅WINDOWS连接)
SqlConnection mysqlconn = new SqlConnection(strConnection); //新建一个Sql连接对象
mysqlconn.Open();
myCmd = new SqlCommand(strSql, mysqlconn);
myCmd.ExecuteNonQuery(); }
}
}
catch
{
MessageBox.Show("导入成功!");
return;
}
}
}
}以上是我写的excel连接导入到数据库的表的代码,有没有更简单的?
希望各路英雄帮帮忙...o(∩_∩)o...