string
connectStr
=
"User
Id=scottPassword=tigerData
Source="
OracleConnection
conn
=
new
OracleConnection(connectStr)
OracleCommand
command
=
new
OracleCommand()
command.Connection
=
conn
conn.Open()
2.通过循环写入大量的数据
for
(int
i
=
0
i
<
recc
i++)
{
string
sql
=
"insert
into
dept
values("
+
i.ToString()
+
","
+
i.ToString()
+
","
+
i.ToString()
+
")"
command.CommandText
=
sql
command.ExecuteNonQuery()
}
//设置一个数据库的连接串
string
connectStr
=
"User
Id=scottPassword=tigerData
Source="
OracleConnection
conn
=
new
OracleConnection(connectStr)
OracleCommand
command
=
new
OracleCommand()
command.Connection
=
conn
//到此为止,还都是我们熟悉的代码,下面就要开始喽
//这个参数需要指定每次批插入的记录数
command.ArrayBindCount
=
recc
//在这个命令行中,用到了参数,参数我们很熟悉,但是这个参数在传值的时候
//用到的是数组,而不是单个的值,这就是它独特的地方
command.CommandText
=
"insert
into
dept
values(:deptno,
:deptname,
:loc)"
conn.Open()
//下面定义几个数组,分别表示三个字段,数组的长度由参数直接给出
int[]
deptNo
=
new
int[recc]
string[]
dname
=
new
string[recc]
string[]
loc
=
new
string[recc]
//
为了传递参数,不可避免的要使用参数,下面会连续定义三个
//
从名称可以直接看出每个参数的含义,不在每个解释了
OracleParameter
deptNoParam
=
new
OracleParameter("deptno",
OracleDbType.Int32)
deptNoParam.Direction
=
ParameterDirection.Input
deptNoParam.Value
=
deptNo
command.Parameters.Add(deptNoParam)
OracleParameter
deptNameParam
=
new
OracleParameter("deptname",
OracleDbType.Varchar2)
deptNameParam.Direction
=
ParameterDirection.Input
deptNameParam.Value
=
dname
command.Parameters.Add(deptNameParam)
OracleParameter
deptLocParam
=
new
OracleParameter("loc",
OracleDbType.Varchar2)
deptLocParam.Direction
=
ParameterDirection.Input
deptLocParam.Value
=
loc
command.Parameters.Add(deptLocParam)
//在下面的循环中,先把数组定义好,而不是像上面那样直接生成SQL
for
(int
i
=
0
i
<
recc
i++)
{
deptNo[i]
=
i
dname[i]
=
i.ToString()
loc[i]
=
i.ToString()
}
//这个调用将把参数数组传进SQL,同时写入数据库
command.ExecuteNonQuery()
参考如下://把数据写入数据库
function res() {
//获取输入值(myname和mymail是两个文本框的id)
var uname = document.getElementById("myname").value
var umail = document.getElementById("mymail").value
//数据库连接对象
var con = new ActiveXObject("ADODB.Connection")
//连接字符串
var str = "DRIVER={SQL SERVER}SERVER=(local)uid=sapwd=123456database=xslt"
//打开数据源
con.Open(str)
//sql语句
var sql = "insert into xslt(name,mail) values(''" + uname + "'',''" + umail + "'')"
try {
//执行sql语句
con.Execute(sql)
//关闭连接
con.Close()
//转向成功页面
window.location.href = "/uploadfile/201101/18/AF93632542.gif" /> }
catch (e) {
alert(e)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)