导航
  • 主页
  • .NET Framework
  • ASP
  • Web Services
  • .NET互联网桌面应用
  • VB
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • AppLauncher
  • 问答

还是OleDb连接数据库命名参数获取的问题:(

jourmen IBM 架构师  2004-04-02 10:43:06
我的代码:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<html>
<head>
<title>delete temp</title>
<script language="C#" runat="server">

OleDbConnection myConnection;

protected void Page_Load(Object sender, EventArgs e)
{
myConnection = new OleDbConnection("Location=\"192.168.0.130\";Provider=\"IBMDADB2.1\";Mode=ReadWrite;User ID=administrator;Data Source=sample;Password=tt");

if (!IsPostBack)
BindGrid("id");
}

protected void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
BindGrid(e.SortExpression);
}

public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
{
String deleteCmd = "delete from staff where id = ?";

OleDbCommand myCommand = new OleDbCommand(deleteCmd, myConnection);
myCommand.Parameters.Add(new OleDbParameter("?", OleDbType.SmallInt, 2));
myCommand.Parameters["?"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];

myCommand.Connection.Open();

try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "提示:已删除记录<br>" + deleteCmd;
Message.Style["color"] = "red";
}
catch (OleDbException)
{
Message.InnerHtml = "错误:未能删除记录";
Message.Style["color"] = "red";
}

myCommand.Connection.Close();

BindGrid("id");
}

public void BindGrid(String sortfield)
{
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from staff", myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds, "staff");

MyDataGrid.DataSource=ds.Tables["staff"].DefaultView;
DataView Source = ds.Tables["staff"].DefaultView;

Source.Sort = sortfield;

MyDataGrid.DataSource=Source;
MyDataGrid.DataBind();
}

</script>
</head>

<body style="font: 10.5pt 宋体">

<form runat="server">

<h3><font face="宋体"><center>删除操作模板:可以删除,排序功能 :)</center></font></h3>

<span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p>

<ASP:DataGrid id="MyDataGrid" runat="server"
Width="900"
BackColor="white"
BorderColor="#E7E7FF"
ShowFooter="false"
CellPadding="3"
CellSpacing="0"
HorizontalAlign="Center"
Font-Name="Courier New"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
HeaderStyle-HorizontalAlign="Center"
DataKeyField="id"
OnDeleteCommand="MyDataGrid_Delete"
OnSortCommand="MyDataGrid_Sort"
AllowSorting="true"
>

<Columns>
<asp:ButtonColumn Text="删除" CommandName="Delete" ItemStyle-Wrap="false"/>
</Columns>

</ASP:DataGrid>

</form>

</body>
</html>


连接是db2 sample数据库,staff表

提示:删除不了

怎么获取到参数呢:(
太郁闷我了:(
...全文
8 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jourmen 2004-04-02
不行:(

myCommand.Parameters.Add( "@id", System.Data.SqlDbType.SmallInt, 2, "id" ).Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];

改成。。。。。。。。。。OleDBType.SmallInt,2,。。。。。。。。也不行

究竟是怎么回事啊???
回复
gOODiDEA 2004-04-02
Try

myCommand.Parameters.Add(new OleDbParameter("?", OleDbType.SmallInt, 2));
myCommand.Parameters["?"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];

===>

myCommand.Parameters.Add( "@id", System.Data.SqlDbType.SmallInt, 2, "id" ).Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];
回复
发动态
发帖子
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
社区公告
暂无公告