以前的ASP程序,操作数据库,修改记录的值,可以用游标进行,如下:
sql="select * from table where id=1"
rs.open sql,conn,1,3
rs("col1")=col1
rs.update
rs.close
我之所以用游标,主要是因为update时,字段里会有非法字符如"'",这样会报错,替换是最简单的,但会造成数据不正确,如果我在字段里存的是HTML代码,替换了就不对了。
.Net里没有游标,象这样的程序,怎么改成.net的程序啊,麻烦用vb.net语法。谢谢。
小弟用的是SQL数据库,麻烦不要用OLEDB,我一直用的是SqlClient,谢谢您把调通的程序发上来,我对这个问题已经发过贴子了,可答案都有错误,谢谢您了。
30 个解决方案
#1
似乎没有游标的东东了。
我是没有用过。
给你一个修改数据库的例子:
'创建一个新的数据命令,连接ORACLE数据库,定义OleDbCommand()
Me.cmdupdatenation = New System.Data.OleDb.OleDbCommand()
'定义并设置命令的属性。
Me.cmdupdatenation.CommandText = "update table set coll='" & coll.tostring() & "'" & " where id=1"
Me.cmdupdatenation.CommandType = CommandType.Text
Me.cmdupdatenation.Connection = Me.OleDbConn
'打开数据库连接
Me.OleDbConn.Open()
'调用ExecuteNonQuery命令,执行无返回值的命令。
Me.cmdupdatenation.ExecuteNonQuery()
Me.OleDbConn.Close()
我是没有用过。
给你一个修改数据库的例子:
'创建一个新的数据命令,连接ORACLE数据库,定义OleDbCommand()
Me.cmdupdatenation = New System.Data.OleDb.OleDbCommand()
'定义并设置命令的属性。
Me.cmdupdatenation.CommandText = "update table set coll='" & coll.tostring() & "'" & " where id=1"
Me.cmdupdatenation.CommandType = CommandType.Text
Me.cmdupdatenation.Connection = Me.OleDbConn
'打开数据库连接
Me.OleDbConn.Open()
'调用ExecuteNonQuery命令,执行无返回值的命令。
Me.cmdupdatenation.ExecuteNonQuery()
Me.OleDbConn.Close()
#2
Public Sub updata()
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("name"))
Dim j As Integer
For j = 1 To 10
dr = dt.NewRow()
dr(0) = "name" + j.ToString()
dt.Rows.Add(dr)
Next
DataGrid2.DataSource = dt
DataGrid2.DataBind()
Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\UserLog.mdb")
conn.Open()
Dim myAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("UPDATE Log SET power=@name", conn)
cmd.Parameters.Add("@name", "name")
cmd.Parameters.Item("@name").SourceColumn = dt.Columns("name").ColumnName
myAdapter.InsertCommand = cmd
myAdapter.Update(dt)
conn.Close()
End Sub
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("name"))
Dim j As Integer
For j = 1 To 10
dr = dt.NewRow()
dr(0) = "name" + j.ToString()
dt.Rows.Add(dr)
Next
DataGrid2.DataSource = dt
DataGrid2.DataBind()
Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\UserLog.mdb")
conn.Open()
Dim myAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("UPDATE Log SET power=@name", conn)
cmd.Parameters.Add("@name", "name")
cmd.Parameters.Item("@name").SourceColumn = dt.Columns("name").ColumnName
myAdapter.InsertCommand = cmd
myAdapter.Update(dt)
conn.Close()
End Sub
#3
大哥,请不要用OLEDB行吗?另外,我写的是ASP.NET程序,是浏览器版本,也就是B/S版的,不是C/S的。谢谢您。
#4
两者区别不大,导入时,import System.Data.SqlClient;
将OleDb换成Sql就可以了,比如OledbConnection换成SqlConnection等
将OleDb换成Sql就可以了,比如OledbConnection换成SqlConnection等
#5
理论上是这样,可我换的时候总是出错。
#6
Public Sub updata()
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("name"))
Dim j As Integer
For j = 1 To 10
dr = dt.NewRow()
dr(0) = "name" + j.ToString()
dt.Rows.Add(dr)
Next
DataGrid2.DataSource = dt
DataGrid2.DataBind()
Dim conn As SqlConnection = New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\UserLog.mdb")
conn.Open()
Dim myAdapter As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand = New SqlCommand("UPDATE Log SET power=@name", conn)
cmd.Parameters.Add("@name", "name")
cmd.Parameters.Item("@name").SourceColumn = dt.Columns("name").ColumnName
myAdapter.InsertCommand = cmd
myAdapter.Update(dt)
conn.Close()
End Sub
这个b/s上可以用的
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("name"))
Dim j As Integer
For j = 1 To 10
dr = dt.NewRow()
dr(0) = "name" + j.ToString()
dt.Rows.Add(dr)
Next
DataGrid2.DataSource = dt
DataGrid2.DataBind()
Dim conn As SqlConnection = New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\UserLog.mdb")
conn.Open()
Dim myAdapter As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand = New SqlCommand("UPDATE Log SET power=@name", conn)
cmd.Parameters.Add("@name", "name")
cmd.Parameters.Item("@name").SourceColumn = dt.Columns("name").ColumnName
myAdapter.InsertCommand = cmd
myAdapter.Update(dt)
conn.Close()
End Sub
这个b/s上可以用的
#7
badelf(曹军) ,我写的就是B/S的啊
#8
有两种方法:
1.一般用于多列
Dim connstr As String = "data source=RAIN;initial catalog=doctors;integrated security=SSPI"
Dim conn As New SqlClient.SqlConnection(connstr)
Dim sql As String = "select * from table where id=1"
Dim da As New SqlClient.SqlDataAdapter(sql, conn)
Dim ds As New DataSet()
da.Fill(ds, "table")
Dim db As New SqlClient.SqlCommandBuilder(da)
Dim dr As DataRow
dr = ds.Tables("table").Rows(0)
dr("coll") = coll
da.Update(ds, "table")
1.一般用于多列
Dim connstr As String = "data source=RAIN;initial catalog=doctors;integrated security=SSPI"
Dim conn As New SqlClient.SqlConnection(connstr)
Dim sql As String = "select * from table where id=1"
Dim da As New SqlClient.SqlDataAdapter(sql, conn)
Dim ds As New DataSet()
da.Fill(ds, "table")
Dim db As New SqlClient.SqlCommandBuilder(da)
Dim dr As DataRow
dr = ds.Tables("table").Rows(0)
dr("coll") = coll
da.Update(ds, "table")
#9
^_^
#10
2 直接用sql语句
Dim upsql As String
upsql = "Update table set coll='" & coll & "'"
conn.Open()
Dim cmdUP As New SqlClient.SqlCommand(upsql, conn)
cmdUP.ExecuteNonQuery()
conn.Close()
Dim upsql As String
upsql = "Update table set coll='" & coll & "'"
conn.Open()
Dim cmdUP As New SqlClient.SqlCommand(upsql, conn)
cmdUP.ExecuteNonQuery()
conn.Close()
#11
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
Dim MyConnection As SqlConnection
Sub Page_Load(Sender As Object, E As EventArgs)
MyConnection = New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")
If Not (IsPostBack)
BindGrid()
End If
End Sub
Sub AddAuthor_Click(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyCommand As SqlCommand
If au_id.Value = "" Or au_fname.Value = "" Or au_lname.Value = "" Or phone.Value = ""
Message.InnerHtml = "错误:""作者 ID""、""姓名""或""电话""不允许使用空值"
Message.Style("color") = "red"
BindGrid()
End If
Dim InsertCmd As String = "insert into Authors (au_id, au_lname, au_fname, phone, address, city, state, zip, contract) values (@Id, @LName, @FName, @Phone, @Address, @City, @State, @Zip, @Contract)"
MyCommand = New SqlCommand(InsertCmd, MyConnection)
MyCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.NVarChar, 11))
MyCommand.Parameters("@Id").Value = au_id.Value
MyCommand.Parameters.Add(New SqlParameter("@LName", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@LName").Value = au_lname.Value
MyCommand.Parameters.Add(New SqlParameter("@FName", SqlDbType.NVarChar, 20))
MyCommand.Parameters("@FName").Value = au_fname.Value
MyCommand.Parameters.Add(New SqlParameter("@Phone", SqlDbType.NChar, 12))
MyCommand.Parameters("@Phone").Value = phone.Value
MyCommand.Parameters.Add(New SqlParameter("@Address", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Address").Value = address.Value
MyCommand.Parameters.Add(New SqlParameter("@City", SqlDbType.NVarChar, 20))
MyCommand.Parameters("@City").Value = city.Value
MyCommand.Parameters.Add(New SqlParameter("@State", SqlDbType.NChar, 2))
MyCommand.Parameters("@State").Value = stateabbr.Value
MyCommand.Parameters.Add(New SqlParameter("@Zip", SqlDbType.NChar, 5))
MyCommand.Parameters("@Zip").Value = zip.Value
MyCommand.Parameters.Add(New SqlParameter("@Contract", SqlDbType.NVarChar,1))
MyCommand.Parameters("@Contract").Value = contract.Value
MyCommand.Connection.Open()
Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>已添加记录</b><br>" & InsertCmd.ToString()
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "错误:已存在具有相同主键的记录"
Else
Message.InnerHtml = "错误:未能添加记录,请确保正确填写了字段"
End If
Message.Style("color") = "red"
End Try
MyCommand.Connection.Close()
BindGrid()
End Sub
Sub BindGrid()
Dim MyCommand As SqlDataAdapter = new SqlDataAdapter("select * from Authors", MyConnection)
Dim DS As DataSet = new DataSet()
MyCommand.Fill(DS, "Authors")
MyDataGrid.DataSource=DS.Tables("Authors").DefaultView
MyDataGrid.DataBind()
End Sub
</script>
<body style="font: 10.5pt 宋体">
<form runat="server">
<h3><font face="宋体">插入数据行</font></h3>
<table width="95%">
<tr>
<td valign="top">
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="宋体"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>
</td>
<td valign="top">
<table style="font: 9pt 宋体">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="font:10.5pt 宋体">添加新作者:</td>
</tr>
<tr>
<td nowrap>作者 ID:</td>
<td><input type="text" id="au_id" value="000-00-0000" runat="server"></td>
</tr>
<tr>
<td nowrap>姓氏:</td>
<td><input type="text" id="au_lname" value="Doe" runat="server"></td>
</tr>
<tr nowrap>
<td>名字:</td>
<td><input type="text" id="au_fname" value="John" runat="server"></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" id="phone" value="808 555-5555" runat="server"></td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" id="address" value="One Microsoft Way" runat="server"></td>
</tr>
<tr>
<td>城市:</td>
<td><input type="text" id="city" value="Redmond" runat="server"></td>
</tr>
<tr>
<td>州:</td>
<td>
<select id="stateabbr" runat="server">
<option>CA</option>
<option>IN</option>
<option>KS</option>
<option>MD</option>
<option>MI</option>
<option>OR</option>
<option>TN</option>
<option>UT</option>
</select>
</td>
</tr>
<tr>
<td nowrap>邮政编码:</td>
<td><input type="text" id="zip" value="98005" runat="server"></td>
</tr>
<tr>
<td>协定:</td>
<td>
<select id="contract" runat="server">
<option value="0">假</option>
<option value="1">真</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td style="padding-top:15">
<input type="submit" OnServerClick="AddAuthor_Click" value="添加作者" runat="server">
</td>
</tr>
<tr>
<td colspan="2" style="padding-top:15" align="center">
<span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
Dim MyConnection As SqlConnection
Sub Page_Load(Sender As Object, E As EventArgs)
MyConnection = New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")
If Not (IsPostBack)
BindGrid()
End If
End Sub
Sub AddAuthor_Click(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyCommand As SqlCommand
If au_id.Value = "" Or au_fname.Value = "" Or au_lname.Value = "" Or phone.Value = ""
Message.InnerHtml = "错误:""作者 ID""、""姓名""或""电话""不允许使用空值"
Message.Style("color") = "red"
BindGrid()
End If
Dim InsertCmd As String = "insert into Authors (au_id, au_lname, au_fname, phone, address, city, state, zip, contract) values (@Id, @LName, @FName, @Phone, @Address, @City, @State, @Zip, @Contract)"
MyCommand = New SqlCommand(InsertCmd, MyConnection)
MyCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.NVarChar, 11))
MyCommand.Parameters("@Id").Value = au_id.Value
MyCommand.Parameters.Add(New SqlParameter("@LName", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@LName").Value = au_lname.Value
MyCommand.Parameters.Add(New SqlParameter("@FName", SqlDbType.NVarChar, 20))
MyCommand.Parameters("@FName").Value = au_fname.Value
MyCommand.Parameters.Add(New SqlParameter("@Phone", SqlDbType.NChar, 12))
MyCommand.Parameters("@Phone").Value = phone.Value
MyCommand.Parameters.Add(New SqlParameter("@Address", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Address").Value = address.Value
MyCommand.Parameters.Add(New SqlParameter("@City", SqlDbType.NVarChar, 20))
MyCommand.Parameters("@City").Value = city.Value
MyCommand.Parameters.Add(New SqlParameter("@State", SqlDbType.NChar, 2))
MyCommand.Parameters("@State").Value = stateabbr.Value
MyCommand.Parameters.Add(New SqlParameter("@Zip", SqlDbType.NChar, 5))
MyCommand.Parameters("@Zip").Value = zip.Value
MyCommand.Parameters.Add(New SqlParameter("@Contract", SqlDbType.NVarChar,1))
MyCommand.Parameters("@Contract").Value = contract.Value
MyCommand.Connection.Open()
Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>已添加记录</b><br>" & InsertCmd.ToString()
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "错误:已存在具有相同主键的记录"
Else
Message.InnerHtml = "错误:未能添加记录,请确保正确填写了字段"
End If
Message.Style("color") = "red"
End Try
MyCommand.Connection.Close()
BindGrid()
End Sub
Sub BindGrid()
Dim MyCommand As SqlDataAdapter = new SqlDataAdapter("select * from Authors", MyConnection)
Dim DS As DataSet = new DataSet()
MyCommand.Fill(DS, "Authors")
MyDataGrid.DataSource=DS.Tables("Authors").DefaultView
MyDataGrid.DataBind()
End Sub
</script>
<body style="font: 10.5pt 宋体">
<form runat="server">
<h3><font face="宋体">插入数据行</font></h3>
<table width="95%">
<tr>
<td valign="top">
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="宋体"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>
</td>
<td valign="top">
<table style="font: 9pt 宋体">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="font:10.5pt 宋体">添加新作者:</td>
</tr>
<tr>
<td nowrap>作者 ID:</td>
<td><input type="text" id="au_id" value="000-00-0000" runat="server"></td>
</tr>
<tr>
<td nowrap>姓氏:</td>
<td><input type="text" id="au_lname" value="Doe" runat="server"></td>
</tr>
<tr nowrap>
<td>名字:</td>
<td><input type="text" id="au_fname" value="John" runat="server"></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" id="phone" value="808 555-5555" runat="server"></td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" id="address" value="One Microsoft Way" runat="server"></td>
</tr>
<tr>
<td>城市:</td>
<td><input type="text" id="city" value="Redmond" runat="server"></td>
</tr>
<tr>
<td>州:</td>
<td>
<select id="stateabbr" runat="server">
<option>CA</option>
<option>IN</option>
<option>KS</option>
<option>MD</option>
<option>MI</option>
<option>OR</option>
<option>TN</option>
<option>UT</option>
</select>
</td>
</tr>
<tr>
<td nowrap>邮政编码:</td>
<td><input type="text" id="zip" value="98005" runat="server"></td>
</tr>
<tr>
<td>协定:</td>
<td>
<select id="contract" runat="server">
<option value="0">假</option>
<option value="1">真</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td style="padding-top:15">
<input type="submit" OnServerClick="AddAuthor_Click" value="添加作者" runat="server">
</td>
</tr>
<tr>
<td colspan="2" style="padding-top:15" align="center">
<span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
#12
Sorry,第二种方法中update语句有误应为
upsql = "Update table set coll='" & coll & "' where id=1"
upsql = "Update table set coll='" & coll & "' where id=1"
#13
chxzll(小气包)
我的程序:
strSql="select * from Human where UserID='" & UserID & "'"
Dim Conn As New SqlClient.SqlConnection(strConn)
Dim Da As New SqlClient.SqlDataAdapter(strSql, Conn)
Dim Ds As New DataSet()
Da.Fill(Ds, "table")
Dim DB As New SqlClient.SqlCommandBuilder(Da)
Dim Dr As DataRow
Dr = Ds.Tables("table").Rows(0)
Dr("Name") = Name
Da.Update(Ds, "table")
运行报错:
对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
源错误:
行 63: Dr = Ds.Tables("table").Rows(0)
行 64: Dr("Name") = Name
行 65: Da.Update(Ds, "table") 就这一行出错
行 66: else
行 67: strSql="select * from Human where ID is Null"
我的程序:
strSql="select * from Human where UserID='" & UserID & "'"
Dim Conn As New SqlClient.SqlConnection(strConn)
Dim Da As New SqlClient.SqlDataAdapter(strSql, Conn)
Dim Ds As New DataSet()
Da.Fill(Ds, "table")
Dim DB As New SqlClient.SqlCommandBuilder(Da)
Dim Dr As DataRow
Dr = Ds.Tables("table").Rows(0)
Dr("Name") = Name
Da.Update(Ds, "table")
运行报错:
对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
源错误:
行 63: Dr = Ds.Tables("table").Rows(0)
行 64: Dr("Name") = Name
行 65: Da.Update(Ds, "table") 就这一行出错
行 66: else
行 67: strSql="select * from Human where ID is Null"
#14
仔细看第1 种方法
Dim db As New SqlClient.SqlCommandBuilder(da)
这句话写了吗?
Dim db As New SqlClient.SqlCommandBuilder(da)
这句话写了吗?
#15
写了,我把我的程序代码都贴出来了,麻烦你帮我看看
#16
将如下代码
Dim da As New SqlClient.SqlDataAdapter(sql, conn)
Dim ds As New DataSet()
da.Fill(ds, "table")
Dim db As New SqlClient.SqlCommandBuilder(da)
改成这样
Dim da As New SqlClient.SqlDataAdapter()
da.SelectCommand = New SqlClient.SqlCommand(sql,conn)
Dim db As New SqlClient.SqlCommandBuilder(da)
试试看
Dim da As New SqlClient.SqlDataAdapter(sql, conn)
Dim ds As New DataSet()
da.Fill(ds, "table")
Dim db As New SqlClient.SqlCommandBuilder(da)
改成这样
Dim da As New SqlClient.SqlDataAdapter()
da.SelectCommand = New SqlClient.SqlCommand(sql,conn)
Dim db As New SqlClient.SqlCommandBuilder(da)
试试看
#17
应该没问题啊,两种方式在我机子上都正常
#18
代码改成这样,故障依旧,错误和上次一模一样
strSql="select * from Human where UserID='" & UserID & "'"
Dim Conn As New SqlClient.SqlConnection(strConn)
Dim Ds As New DataSet()
Dim Da As New SqlClient.SqlDataAdapter()
Da.SelectCommand = New SqlClient.SqlCommand(strSql,Conn)
Da.Fill(Ds, "table")
Dim DB As New SqlClient.SqlCommandBuilder(Da)
Dim Dr As DataRow
Dr = Ds.Tables("table").Rows(0)
Dr("Name") = Name
Da.Update(Ds, "table")
strSql="select * from Human where UserID='" & UserID & "'"
Dim Conn As New SqlClient.SqlConnection(strConn)
Dim Ds As New DataSet()
Dim Da As New SqlClient.SqlDataAdapter()
Da.SelectCommand = New SqlClient.SqlCommand(strSql,Conn)
Da.Fill(Ds, "table")
Dim DB As New SqlClient.SqlCommandBuilder(Da)
Dim Dr As DataRow
Dr = Ds.Tables("table").Rows(0)
Dr("Name") = Name
Da.Update(Ds, "table")
#19
我的连接字符串是:
"DATA SOURCE=(local);UID=OA;PWD=admin;DATABASE=OA;connect timeout=120"
"DATA SOURCE=(local);UID=OA;PWD=admin;DATABASE=OA;connect timeout=120"
#20
Da.Update(Ds, "table")
改为
Da.Update(Ds, "Human")
改为
Da.Update(Ds, "Human")
#21
第二种方式肯定可以的,我也用过,但我不能用这种方式,只能用第一种方式,并且还不能用OLEDB。
#22
上面是:
Da.Fill(Ds, "table")
下面应该也是表table啊,而不应该是Human吧。
Da.Fill(Ds, "table")
下面应该也是表table啊,而不应该是Human吧。
#23
strSql="select * from Human where UserID='" & UserID & "'"
说明你的表名是Human啊,所以两句都要改
Da.Fill(Ds, "table")
.
.
.
Da.Update(Ds, "Human")
说明你的表名是Human啊,所以两句都要改
Da.Fill(Ds, "table")
.
.
.
Da.Update(Ds, "Human")
#24
Sorry,写错了,
strSql="select * from Human where UserID='" & UserID & "'"
说明你的表名是Human啊,所以两句都要改
Da.Fill(Ds, "Human")
.
.
.
Da.Update(Ds, "Human")
strSql="select * from Human where UserID='" & UserID & "'"
说明你的表名是Human啊,所以两句都要改
Da.Fill(Ds, "Human")
.
.
.
Da.Update(Ds, "Human")
#25
按您的提示改了代码,错误类型和以前一样。
值得注意的有一点。Da.Fille(DS,"表名"),这一句里的表名,是可以任意指定的,和SQL语句里没有任何关系,包括UPDATE的时候,和SQL语句也没关系,所以不是Human和Table的问题。
错误是:
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
还是那一句
Da.Update(Ds, "Human")
出错
值得注意的有一点。Da.Fille(DS,"表名"),这一句里的表名,是可以任意指定的,和SQL语句里没有任何关系,包括UPDATE的时候,和SQL语句也没关系,所以不是Human和Table的问题。
错误是:
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
还是那一句
Da.Update(Ds, "Human")
出错
#26
你确定写update语句能正常修改吗?
#27
Sorry,我这儿没遇到过那样的问题,我告诉你一个调试的办法吧,在解决方案资源管理器中,右击该页,选择生成并浏览,然后在.net内嵌的浏览器中浏览该页,出错后点F1,看.net给你的诊断信息是什么
#28
你没有任何update语句
#29
他有啊
Dr("Name") = Name
这句就是
Dr("Name") = Name
这句就是
#30
就是不成功啊,怎么回事啊。
我主要是喜欢chxzll(小气包) 的这种写法,这种写法简单,如果有别的方法,也可以啊,只要代码不是使用OLEDB的,并且是VB.Net的代码,只要能完成这个功能,都可以了。
5~~~~~~~~~~~~~~~~~
我主要是喜欢chxzll(小气包) 的这种写法,这种写法简单,如果有别的方法,也可以啊,只要代码不是使用OLEDB的,并且是VB.Net的代码,只要能完成这个功能,都可以了。
5~~~~~~~~~~~~~~~~~
#1
似乎没有游标的东东了。
我是没有用过。
给你一个修改数据库的例子:
'创建一个新的数据命令,连接ORACLE数据库,定义OleDbCommand()
Me.cmdupdatenation = New System.Data.OleDb.OleDbCommand()
'定义并设置命令的属性。
Me.cmdupdatenation.CommandText = "update table set coll='" & coll.tostring() & "'" & " where id=1"
Me.cmdupdatenation.CommandType = CommandType.Text
Me.cmdupdatenation.Connection = Me.OleDbConn
'打开数据库连接
Me.OleDbConn.Open()
'调用ExecuteNonQuery命令,执行无返回值的命令。
Me.cmdupdatenation.ExecuteNonQuery()
Me.OleDbConn.Close()
我是没有用过。
给你一个修改数据库的例子:
'创建一个新的数据命令,连接ORACLE数据库,定义OleDbCommand()
Me.cmdupdatenation = New System.Data.OleDb.OleDbCommand()
'定义并设置命令的属性。
Me.cmdupdatenation.CommandText = "update table set coll='" & coll.tostring() & "'" & " where id=1"
Me.cmdupdatenation.CommandType = CommandType.Text
Me.cmdupdatenation.Connection = Me.OleDbConn
'打开数据库连接
Me.OleDbConn.Open()
'调用ExecuteNonQuery命令,执行无返回值的命令。
Me.cmdupdatenation.ExecuteNonQuery()
Me.OleDbConn.Close()
#2
Public Sub updata()
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("name"))
Dim j As Integer
For j = 1 To 10
dr = dt.NewRow()
dr(0) = "name" + j.ToString()
dt.Rows.Add(dr)
Next
DataGrid2.DataSource = dt
DataGrid2.DataBind()
Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\UserLog.mdb")
conn.Open()
Dim myAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("UPDATE Log SET power=@name", conn)
cmd.Parameters.Add("@name", "name")
cmd.Parameters.Item("@name").SourceColumn = dt.Columns("name").ColumnName
myAdapter.InsertCommand = cmd
myAdapter.Update(dt)
conn.Close()
End Sub
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("name"))
Dim j As Integer
For j = 1 To 10
dr = dt.NewRow()
dr(0) = "name" + j.ToString()
dt.Rows.Add(dr)
Next
DataGrid2.DataSource = dt
DataGrid2.DataBind()
Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\UserLog.mdb")
conn.Open()
Dim myAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("UPDATE Log SET power=@name", conn)
cmd.Parameters.Add("@name", "name")
cmd.Parameters.Item("@name").SourceColumn = dt.Columns("name").ColumnName
myAdapter.InsertCommand = cmd
myAdapter.Update(dt)
conn.Close()
End Sub
#3
大哥,请不要用OLEDB行吗?另外,我写的是ASP.NET程序,是浏览器版本,也就是B/S版的,不是C/S的。谢谢您。
#4
两者区别不大,导入时,import System.Data.SqlClient;
将OleDb换成Sql就可以了,比如OledbConnection换成SqlConnection等
将OleDb换成Sql就可以了,比如OledbConnection换成SqlConnection等
#5
理论上是这样,可我换的时候总是出错。
#6
Public Sub updata()
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("name"))
Dim j As Integer
For j = 1 To 10
dr = dt.NewRow()
dr(0) = "name" + j.ToString()
dt.Rows.Add(dr)
Next
DataGrid2.DataSource = dt
DataGrid2.DataBind()
Dim conn As SqlConnection = New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\UserLog.mdb")
conn.Open()
Dim myAdapter As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand = New SqlCommand("UPDATE Log SET power=@name", conn)
cmd.Parameters.Add("@name", "name")
cmd.Parameters.Item("@name").SourceColumn = dt.Columns("name").ColumnName
myAdapter.InsertCommand = cmd
myAdapter.Update(dt)
conn.Close()
End Sub
这个b/s上可以用的
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("name"))
Dim j As Integer
For j = 1 To 10
dr = dt.NewRow()
dr(0) = "name" + j.ToString()
dt.Rows.Add(dr)
Next
DataGrid2.DataSource = dt
DataGrid2.DataBind()
Dim conn As SqlConnection = New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\UserLog.mdb")
conn.Open()
Dim myAdapter As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand = New SqlCommand("UPDATE Log SET power=@name", conn)
cmd.Parameters.Add("@name", "name")
cmd.Parameters.Item("@name").SourceColumn = dt.Columns("name").ColumnName
myAdapter.InsertCommand = cmd
myAdapter.Update(dt)
conn.Close()
End Sub
这个b/s上可以用的
#7
badelf(曹军) ,我写的就是B/S的啊
#8
有两种方法:
1.一般用于多列
Dim connstr As String = "data source=RAIN;initial catalog=doctors;integrated security=SSPI"
Dim conn As New SqlClient.SqlConnection(connstr)
Dim sql As String = "select * from table where id=1"
Dim da As New SqlClient.SqlDataAdapter(sql, conn)
Dim ds As New DataSet()
da.Fill(ds, "table")
Dim db As New SqlClient.SqlCommandBuilder(da)
Dim dr As DataRow
dr = ds.Tables("table").Rows(0)
dr("coll") = coll
da.Update(ds, "table")
1.一般用于多列
Dim connstr As String = "data source=RAIN;initial catalog=doctors;integrated security=SSPI"
Dim conn As New SqlClient.SqlConnection(connstr)
Dim sql As String = "select * from table where id=1"
Dim da As New SqlClient.SqlDataAdapter(sql, conn)
Dim ds As New DataSet()
da.Fill(ds, "table")
Dim db As New SqlClient.SqlCommandBuilder(da)
Dim dr As DataRow
dr = ds.Tables("table").Rows(0)
dr("coll") = coll
da.Update(ds, "table")
#9
^_^
#10
2 直接用sql语句
Dim upsql As String
upsql = "Update table set coll='" & coll & "'"
conn.Open()
Dim cmdUP As New SqlClient.SqlCommand(upsql, conn)
cmdUP.ExecuteNonQuery()
conn.Close()
Dim upsql As String
upsql = "Update table set coll='" & coll & "'"
conn.Open()
Dim cmdUP As New SqlClient.SqlCommand(upsql, conn)
cmdUP.ExecuteNonQuery()
conn.Close()
#11
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
Dim MyConnection As SqlConnection
Sub Page_Load(Sender As Object, E As EventArgs)
MyConnection = New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")
If Not (IsPostBack)
BindGrid()
End If
End Sub
Sub AddAuthor_Click(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyCommand As SqlCommand
If au_id.Value = "" Or au_fname.Value = "" Or au_lname.Value = "" Or phone.Value = ""
Message.InnerHtml = "错误:""作者 ID""、""姓名""或""电话""不允许使用空值"
Message.Style("color") = "red"
BindGrid()
End If
Dim InsertCmd As String = "insert into Authors (au_id, au_lname, au_fname, phone, address, city, state, zip, contract) values (@Id, @LName, @FName, @Phone, @Address, @City, @State, @Zip, @Contract)"
MyCommand = New SqlCommand(InsertCmd, MyConnection)
MyCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.NVarChar, 11))
MyCommand.Parameters("@Id").Value = au_id.Value
MyCommand.Parameters.Add(New SqlParameter("@LName", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@LName").Value = au_lname.Value
MyCommand.Parameters.Add(New SqlParameter("@FName", SqlDbType.NVarChar, 20))
MyCommand.Parameters("@FName").Value = au_fname.Value
MyCommand.Parameters.Add(New SqlParameter("@Phone", SqlDbType.NChar, 12))
MyCommand.Parameters("@Phone").Value = phone.Value
MyCommand.Parameters.Add(New SqlParameter("@Address", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Address").Value = address.Value
MyCommand.Parameters.Add(New SqlParameter("@City", SqlDbType.NVarChar, 20))
MyCommand.Parameters("@City").Value = city.Value
MyCommand.Parameters.Add(New SqlParameter("@State", SqlDbType.NChar, 2))
MyCommand.Parameters("@State").Value = stateabbr.Value
MyCommand.Parameters.Add(New SqlParameter("@Zip", SqlDbType.NChar, 5))
MyCommand.Parameters("@Zip").Value = zip.Value
MyCommand.Parameters.Add(New SqlParameter("@Contract", SqlDbType.NVarChar,1))
MyCommand.Parameters("@Contract").Value = contract.Value
MyCommand.Connection.Open()
Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>已添加记录</b><br>" & InsertCmd.ToString()
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "错误:已存在具有相同主键的记录"
Else
Message.InnerHtml = "错误:未能添加记录,请确保正确填写了字段"
End If
Message.Style("color") = "red"
End Try
MyCommand.Connection.Close()
BindGrid()
End Sub
Sub BindGrid()
Dim MyCommand As SqlDataAdapter = new SqlDataAdapter("select * from Authors", MyConnection)
Dim DS As DataSet = new DataSet()
MyCommand.Fill(DS, "Authors")
MyDataGrid.DataSource=DS.Tables("Authors").DefaultView
MyDataGrid.DataBind()
End Sub
</script>
<body style="font: 10.5pt 宋体">
<form runat="server">
<h3><font face="宋体">插入数据行</font></h3>
<table width="95%">
<tr>
<td valign="top">
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="宋体"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>
</td>
<td valign="top">
<table style="font: 9pt 宋体">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="font:10.5pt 宋体">添加新作者:</td>
</tr>
<tr>
<td nowrap>作者 ID:</td>
<td><input type="text" id="au_id" value="000-00-0000" runat="server"></td>
</tr>
<tr>
<td nowrap>姓氏:</td>
<td><input type="text" id="au_lname" value="Doe" runat="server"></td>
</tr>
<tr nowrap>
<td>名字:</td>
<td><input type="text" id="au_fname" value="John" runat="server"></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" id="phone" value="808 555-5555" runat="server"></td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" id="address" value="One Microsoft Way" runat="server"></td>
</tr>
<tr>
<td>城市:</td>
<td><input type="text" id="city" value="Redmond" runat="server"></td>
</tr>
<tr>
<td>州:</td>
<td>
<select id="stateabbr" runat="server">
<option>CA</option>
<option>IN</option>
<option>KS</option>
<option>MD</option>
<option>MI</option>
<option>OR</option>
<option>TN</option>
<option>UT</option>
</select>
</td>
</tr>
<tr>
<td nowrap>邮政编码:</td>
<td><input type="text" id="zip" value="98005" runat="server"></td>
</tr>
<tr>
<td>协定:</td>
<td>
<select id="contract" runat="server">
<option value="0">假</option>
<option value="1">真</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td style="padding-top:15">
<input type="submit" OnServerClick="AddAuthor_Click" value="添加作者" runat="server">
</td>
</tr>
<tr>
<td colspan="2" style="padding-top:15" align="center">
<span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
Dim MyConnection As SqlConnection
Sub Page_Load(Sender As Object, E As EventArgs)
MyConnection = New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")
If Not (IsPostBack)
BindGrid()
End If
End Sub
Sub AddAuthor_Click(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyCommand As SqlCommand
If au_id.Value = "" Or au_fname.Value = "" Or au_lname.Value = "" Or phone.Value = ""
Message.InnerHtml = "错误:""作者 ID""、""姓名""或""电话""不允许使用空值"
Message.Style("color") = "red"
BindGrid()
End If
Dim InsertCmd As String = "insert into Authors (au_id, au_lname, au_fname, phone, address, city, state, zip, contract) values (@Id, @LName, @FName, @Phone, @Address, @City, @State, @Zip, @Contract)"
MyCommand = New SqlCommand(InsertCmd, MyConnection)
MyCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.NVarChar, 11))
MyCommand.Parameters("@Id").Value = au_id.Value
MyCommand.Parameters.Add(New SqlParameter("@LName", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@LName").Value = au_lname.Value
MyCommand.Parameters.Add(New SqlParameter("@FName", SqlDbType.NVarChar, 20))
MyCommand.Parameters("@FName").Value = au_fname.Value
MyCommand.Parameters.Add(New SqlParameter("@Phone", SqlDbType.NChar, 12))
MyCommand.Parameters("@Phone").Value = phone.Value
MyCommand.Parameters.Add(New SqlParameter("@Address", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Address").Value = address.Value
MyCommand.Parameters.Add(New SqlParameter("@City", SqlDbType.NVarChar, 20))
MyCommand.Parameters("@City").Value = city.Value
MyCommand.Parameters.Add(New SqlParameter("@State", SqlDbType.NChar, 2))
MyCommand.Parameters("@State").Value = stateabbr.Value
MyCommand.Parameters.Add(New SqlParameter("@Zip", SqlDbType.NChar, 5))
MyCommand.Parameters("@Zip").Value = zip.Value
MyCommand.Parameters.Add(New SqlParameter("@Contract", SqlDbType.NVarChar,1))
MyCommand.Parameters("@Contract").Value = contract.Value
MyCommand.Connection.Open()
Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>已添加记录</b><br>" & InsertCmd.ToString()
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "错误:已存在具有相同主键的记录"
Else
Message.InnerHtml = "错误:未能添加记录,请确保正确填写了字段"
End If
Message.Style("color") = "red"
End Try
MyCommand.Connection.Close()
BindGrid()
End Sub
Sub BindGrid()
Dim MyCommand As SqlDataAdapter = new SqlDataAdapter("select * from Authors", MyConnection)
Dim DS As DataSet = new DataSet()
MyCommand.Fill(DS, "Authors")
MyDataGrid.DataSource=DS.Tables("Authors").DefaultView
MyDataGrid.DataBind()
End Sub
</script>
<body style="font: 10.5pt 宋体">
<form runat="server">
<h3><font face="宋体">插入数据行</font></h3>
<table width="95%">
<tr>
<td valign="top">
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="宋体"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>
</td>
<td valign="top">
<table style="font: 9pt 宋体">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="font:10.5pt 宋体">添加新作者:</td>
</tr>
<tr>
<td nowrap>作者 ID:</td>
<td><input type="text" id="au_id" value="000-00-0000" runat="server"></td>
</tr>
<tr>
<td nowrap>姓氏:</td>
<td><input type="text" id="au_lname" value="Doe" runat="server"></td>
</tr>
<tr nowrap>
<td>名字:</td>
<td><input type="text" id="au_fname" value="John" runat="server"></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" id="phone" value="808 555-5555" runat="server"></td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" id="address" value="One Microsoft Way" runat="server"></td>
</tr>
<tr>
<td>城市:</td>
<td><input type="text" id="city" value="Redmond" runat="server"></td>
</tr>
<tr>
<td>州:</td>
<td>
<select id="stateabbr" runat="server">
<option>CA</option>
<option>IN</option>
<option>KS</option>
<option>MD</option>
<option>MI</option>
<option>OR</option>
<option>TN</option>
<option>UT</option>
</select>
</td>
</tr>
<tr>
<td nowrap>邮政编码:</td>
<td><input type="text" id="zip" value="98005" runat="server"></td>
</tr>
<tr>
<td>协定:</td>
<td>
<select id="contract" runat="server">
<option value="0">假</option>
<option value="1">真</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td style="padding-top:15">
<input type="submit" OnServerClick="AddAuthor_Click" value="添加作者" runat="server">
</td>
</tr>
<tr>
<td colspan="2" style="padding-top:15" align="center">
<span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
#12
Sorry,第二种方法中update语句有误应为
upsql = "Update table set coll='" & coll & "' where id=1"
upsql = "Update table set coll='" & coll & "' where id=1"
#13
chxzll(小气包)
我的程序:
strSql="select * from Human where UserID='" & UserID & "'"
Dim Conn As New SqlClient.SqlConnection(strConn)
Dim Da As New SqlClient.SqlDataAdapter(strSql, Conn)
Dim Ds As New DataSet()
Da.Fill(Ds, "table")
Dim DB As New SqlClient.SqlCommandBuilder(Da)
Dim Dr As DataRow
Dr = Ds.Tables("table").Rows(0)
Dr("Name") = Name
Da.Update(Ds, "table")
运行报错:
对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
源错误:
行 63: Dr = Ds.Tables("table").Rows(0)
行 64: Dr("Name") = Name
行 65: Da.Update(Ds, "table") 就这一行出错
行 66: else
行 67: strSql="select * from Human where ID is Null"
我的程序:
strSql="select * from Human where UserID='" & UserID & "'"
Dim Conn As New SqlClient.SqlConnection(strConn)
Dim Da As New SqlClient.SqlDataAdapter(strSql, Conn)
Dim Ds As New DataSet()
Da.Fill(Ds, "table")
Dim DB As New SqlClient.SqlCommandBuilder(Da)
Dim Dr As DataRow
Dr = Ds.Tables("table").Rows(0)
Dr("Name") = Name
Da.Update(Ds, "table")
运行报错:
对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
源错误:
行 63: Dr = Ds.Tables("table").Rows(0)
行 64: Dr("Name") = Name
行 65: Da.Update(Ds, "table") 就这一行出错
行 66: else
行 67: strSql="select * from Human where ID is Null"
#14
仔细看第1 种方法
Dim db As New SqlClient.SqlCommandBuilder(da)
这句话写了吗?
Dim db As New SqlClient.SqlCommandBuilder(da)
这句话写了吗?
#15
写了,我把我的程序代码都贴出来了,麻烦你帮我看看
#16
将如下代码
Dim da As New SqlClient.SqlDataAdapter(sql, conn)
Dim ds As New DataSet()
da.Fill(ds, "table")
Dim db As New SqlClient.SqlCommandBuilder(da)
改成这样
Dim da As New SqlClient.SqlDataAdapter()
da.SelectCommand = New SqlClient.SqlCommand(sql,conn)
Dim db As New SqlClient.SqlCommandBuilder(da)
试试看
Dim da As New SqlClient.SqlDataAdapter(sql, conn)
Dim ds As New DataSet()
da.Fill(ds, "table")
Dim db As New SqlClient.SqlCommandBuilder(da)
改成这样
Dim da As New SqlClient.SqlDataAdapter()
da.SelectCommand = New SqlClient.SqlCommand(sql,conn)
Dim db As New SqlClient.SqlCommandBuilder(da)
试试看
#17
应该没问题啊,两种方式在我机子上都正常
#18
代码改成这样,故障依旧,错误和上次一模一样
strSql="select * from Human where UserID='" & UserID & "'"
Dim Conn As New SqlClient.SqlConnection(strConn)
Dim Ds As New DataSet()
Dim Da As New SqlClient.SqlDataAdapter()
Da.SelectCommand = New SqlClient.SqlCommand(strSql,Conn)
Da.Fill(Ds, "table")
Dim DB As New SqlClient.SqlCommandBuilder(Da)
Dim Dr As DataRow
Dr = Ds.Tables("table").Rows(0)
Dr("Name") = Name
Da.Update(Ds, "table")
strSql="select * from Human where UserID='" & UserID & "'"
Dim Conn As New SqlClient.SqlConnection(strConn)
Dim Ds As New DataSet()
Dim Da As New SqlClient.SqlDataAdapter()
Da.SelectCommand = New SqlClient.SqlCommand(strSql,Conn)
Da.Fill(Ds, "table")
Dim DB As New SqlClient.SqlCommandBuilder(Da)
Dim Dr As DataRow
Dr = Ds.Tables("table").Rows(0)
Dr("Name") = Name
Da.Update(Ds, "table")
#19
我的连接字符串是:
"DATA SOURCE=(local);UID=OA;PWD=admin;DATABASE=OA;connect timeout=120"
"DATA SOURCE=(local);UID=OA;PWD=admin;DATABASE=OA;connect timeout=120"
#20
Da.Update(Ds, "table")
改为
Da.Update(Ds, "Human")
改为
Da.Update(Ds, "Human")
#21
第二种方式肯定可以的,我也用过,但我不能用这种方式,只能用第一种方式,并且还不能用OLEDB。
#22
上面是:
Da.Fill(Ds, "table")
下面应该也是表table啊,而不应该是Human吧。
Da.Fill(Ds, "table")
下面应该也是表table啊,而不应该是Human吧。
#23
strSql="select * from Human where UserID='" & UserID & "'"
说明你的表名是Human啊,所以两句都要改
Da.Fill(Ds, "table")
.
.
.
Da.Update(Ds, "Human")
说明你的表名是Human啊,所以两句都要改
Da.Fill(Ds, "table")
.
.
.
Da.Update(Ds, "Human")
#24
Sorry,写错了,
strSql="select * from Human where UserID='" & UserID & "'"
说明你的表名是Human啊,所以两句都要改
Da.Fill(Ds, "Human")
.
.
.
Da.Update(Ds, "Human")
strSql="select * from Human where UserID='" & UserID & "'"
说明你的表名是Human啊,所以两句都要改
Da.Fill(Ds, "Human")
.
.
.
Da.Update(Ds, "Human")
#25
按您的提示改了代码,错误类型和以前一样。
值得注意的有一点。Da.Fille(DS,"表名"),这一句里的表名,是可以任意指定的,和SQL语句里没有任何关系,包括UPDATE的时候,和SQL语句也没关系,所以不是Human和Table的问题。
错误是:
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
还是那一句
Da.Update(Ds, "Human")
出错
值得注意的有一点。Da.Fille(DS,"表名"),这一句里的表名,是可以任意指定的,和SQL语句里没有任何关系,包括UPDATE的时候,和SQL语句也没关系,所以不是Human和Table的问题。
错误是:
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
还是那一句
Da.Update(Ds, "Human")
出错
#26
你确定写update语句能正常修改吗?
#27
Sorry,我这儿没遇到过那样的问题,我告诉你一个调试的办法吧,在解决方案资源管理器中,右击该页,选择生成并浏览,然后在.net内嵌的浏览器中浏览该页,出错后点F1,看.net给你的诊断信息是什么
#28
你没有任何update语句
#29
他有啊
Dr("Name") = Name
这句就是
Dr("Name") = Name
这句就是
#30
就是不成功啊,怎么回事啊。
我主要是喜欢chxzll(小气包) 的这种写法,这种写法简单,如果有别的方法,也可以啊,只要代码不是使用OLEDB的,并且是VB.Net的代码,只要能完成这个功能,都可以了。
5~~~~~~~~~~~~~~~~~
我主要是喜欢chxzll(小气包) 的这种写法,这种写法简单,如果有别的方法,也可以啊,只要代码不是使用OLEDB的,并且是VB.Net的代码,只要能完成这个功能,都可以了。
5~~~~~~~~~~~~~~~~~