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

jourmen 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表

提示:删除不了

怎么获取到参数呢:(
太郁闷我了:(
...全文
73 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
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];

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧