大家帮个忙! 小弟在这谢啦
我把c盘下EXCEL里的学生成绩表调到了datagrid里面啦! 怎样能把数据保存到SQL2000里面特定的学生成绩表 里面! 希望大家能给个列子! 我好套用下! 急用! 才这样! 谢谢大家 --------------------编程问答-------------------- 你把你的代码贴出来呀,--------------------编程问答-------------------- Dim lcConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
lcConnStr = lcConnStr & Trim(myfilename) & _
";Extended Properties=Excel 8.0;"
Dim lobjConn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(lcConnStr)
Dim lobjAdapter As System.Data.OleDb.OleDbDataAdapter
Dim lcSql As String = "select * from [Sheet1$]"
lobjConn.Open()
lobjAdapter = New System.Data.OleDb.OleDbDataAdapter(lcSql, lobjConn)
Dim ds As System.Data.DataSet = New DataSet
lobjAdapter.Fill(ds)
DataGrid1.DataSource = ds.Tables(0) --------------------编程问答-------------------- 这就是能显示出来 在datagrid里面! 怎么保存到SQL? --------------------编程问答-------------------- 你的意思只是对数据的保存?还是用代码实现?
如果只对数据保存,可以直接把EXCEL的数据导入到SQL里面,
我用的SQL2005,数据库名字 右键 快捷键TI,之后选中要导的文件路径,一直下去就可以了。
如果代码实现就麻烦点了,可以用sql insert到表里。你可以把业务再说明白点。 --------------------编程问答-------------------- 是用代码实现! 吧datagird 里面的数据写到SQL表里面 --------------------编程问答--------------------
--------------------编程问答--------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim j As Integer
Dim strTemp As String = ""
Dim sqlCommandTxt As String
Dim constr As String = "User ID=sa;pwd=a;Initial Catalog=zbb;Data Source=(local)" '定义一个变量,连接字符串
Dim sqlstr As String = "" '定义一个变量,SQL语句
Dim mycon As New SqlConnection(constr) '定义一个变量,为一个连接对象,并给定连接字符串。
Dim mycom As New SqlCommand(sqlstr, mycon) '定义一个变量,为命令对象
'添加入库##########################################
For j = 0 To DataGridView1.RowCount
For i = 0 To DataGridView1.ColumnCount - 1
strTemp = strTemp & "'" & DataGridView1.Rows(0).Cells(i).Value & "',"
Next
strTemp = strTemp.Substring(0, strTemp.Length - 1)
sqlCommandTxt = "insert into yourDB (fname1,fname2,fname3) values(" & strTemp & ")"
'MsgBox(strTemp)
mycom.CommandText = sqlCommandTxt
If mycon.State = 1 Then mycon.Close()
mycom.Connection.ConnectionString = constr '为这个连接字符串重新赋值
mycon.Open() '重新打开这个联接
mycom.ExecuteNonQuery() '用不返回结果的查询语句来插入一条记录
MsgBox("已易做图本加入数据库!")
mycon.Close()
Next
'##########################################
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strXMLData As String = String.Empty 'XML文
Dim constr As String = "Data Source=localhost;database=DEMO;Integrated Security=SSPI;" '定义一个变量,连接字符串
Dim mycon As New SqlClient.SqlConnection(constr) '定义一个变量,为一个连接对象,并给定连接字符串。
Dim mycom As New SqlClient.SqlCommand("STORE", mycon) '数据库名字
Dim mypara As New SqlClient.SqlParameter("@strXMLData", SqlDbType.Xml)‘传递参数
strXMLData = dsData.GetXml()'将dataset生成xml
mycom.CommandType = CommandType.StoredProcedure '定义类型为存储过程
mypara.Value = strXMLData '给参数赋值
mycom.Parameters.Add(mypara) '追加参数,类似可以追加其他参数
mycon.Open()
mycom.ExecuteNonQuery()
MsgBox("已易做图本加入数据库!")
mycon.Close()
End Sub
存储过程(里面的重复CHECK,这些你自己加吧,因为我的表里没设主键)
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[STORE]
@strXMLData XML
AS
SET NOCOUNT ON;
DECLARE @RETURN INT; -- 戻り値(0:正常終了,1:チェックエラーがあり, 9:異常終了)
DECLARE @DOCHANDLE INT;
DECLARE @A1 VARCHAR(10);
DECLARE @A2 VARCHAR(10);
DECLARE @A3 VARCHAR(10);
DECLARE @A4 VARCHAR(10);
BEGIN TRY
SET @RETURN = 0;
EXECUTE SP_XML_PREPAREDOCUMENT @DOCHANDLE OUTPUT, @strXMLData;
DECLARE ROWCURSOR CURSOR SCROLL FOR
SELECT
a1
,a2
,a3
,a4
FROM OPENXML(@DOCHANDLE, '/DataSet1/DataTable1', 2)
WITH (
a1 vARCHAR(10)
,a2 VARCHAR(10)
,a3 VARCHAR(10)
,a4 VARCHAR(10))
OPEN ROWCURSOR;
FETCH NEXT FROM ROWCURSOR
INTO
@A1
,@A2
,@A3
,@A4
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO
b
(a1,
a2,
a3,
a4)
VALUES
(@A1,
@A2,
@A3,
@A4)
FETCH NEXT FROM ROWCURSOR
INTO
@A1
,@A2
,@A3
,@A4
END
END TRY
BEGIN CATCH
CLOSE ROWCURSOR;
DEALLOCATE ROWCURSOR;
SET @RETURN = 9;
RETURN @RETURN;
END CATCH
补充:.NET技术 , VB.NET