请教各位老师
有关于选定ddl中的一个值然后点button就将这个值插入数据库的源码吗!~!~
我知道这个核心是update table set XXX=AAA where ID=XXX 实际上就是执行这个语句
但是好久不写代码了 忘记了~!~!
谢谢各位老师能提供给我一份完整的关于这个问题的代码吗?
感激不尽啊~!~!
有关于选定ddl中的一个值然后点button就将这个值插入数据库的源码吗!~!~
我知道这个核心是update table set XXX=AAA where ID=XXX 实际上就是执行这个语句
但是好久不写代码了 忘记了~!~!
谢谢各位老师能提供给我一份完整的关于这个问题的代码吗?
感激不尽啊~!~!
提示
sqlCommand 关键字 ExceuteNonQuery
我有急用
就是在button控件里面代码啊!~!~
你那个是更新
插入是insert into biaoming (字段名) vaules (要插入的值)
利用下拉和button往数据库里插如一个下拉的value就可以了啊
SqlCommand sc = new SqlCommand("sql语句", con);
sc.ExecuteNonQuery();
sqlconnection conn = new sqlconnection(strsqlconnection);
conn.open();
strValue = ddl.selectedValue.ToString();
string strsql ="update table set XXX='"+strValue +"'where ID=XXX ";
sqlcommand cmd = new sqlcommand(strsql,conn);
cmd.ExecuteNonQuery();
conn.closed();
上下文中 不存在strValue啊为什么我连接的远程数据库
语句是sqlconnection conn =new sqlconnection("server=xxx.xxx.xxx.xxx;database=xxx;user ID=sa;pwd=xx")
下面紧接着conn.open()总是报无法连接远程的数据库呢!我数据库是能连的啊,我都绑定了一个gridview了!为什么还是报无法访问呢!
{
try
{
using(SqlConnection con = new SqlConnection("数据库连接字符串"))
{
con.Open();
string val = this.DropDownList1.SelectedItem.Text.Trim(); //获取下拉列表框中选中的值
string strSQL = "update 表名 set 字段名='"+val+"' where ID=1";
using(SqlCommand cmd = new SqlCommand(strSQL,con))
{
cmd.ExecuteNonQuery();
ClientScript.RegisterClientScriptBlock(GetType(),"","<script>alert('数据修改成功!');</script>");
}
}
}
catch(Exception ex)
{
ClientScript.RegisterClientScriptBlock(GetType(),"","<script>alert('数据修改失败,原因是:"+ex.Message+"');</script>");
}
}
就是这个ID是自动生成的 按顺序排列下来的 不能定死的
在修改之前不知道这个ID是多少的
那这个where后面该怎么写啊
看你标题,说是插入数据库,那么给你个增加一条记录的方法 //获取表中最大记录ID
public int getMaxID()
{
DataSet ds = new DataSet();
int ret = 0;
using(SqlConnection con = new SqlConnection("数据库连接字符串"))
{
con.Open();
string strSQL = "select max(ID) as max_id from 表名";
using(SqlDataAdapter adapter= new SqlDataAdapter (strSQL,con))
{
adapter.Fill(ds);
ret = int.Parse(ds.Tables[0].Rows[0]["max_id"].ToString());
}
}
return ret;
}
public void Button1_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection("数据库连接字符串"))
{
con.Open();
string val = this.DropDownList1.SelectedItem.Text.Trim(); //获取下拉列表框中选中的值
int id = getMaxID();
string strSQL = "insert into 表名 values(" +id+",'"+val+"')";
using (SqlCommand cmd = new SqlCommand(strSQL, con))
{
cmd.ExecuteNonQuery();
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('数据添加成功!');</script>");
}
}
}
catch (Exception ex)
{
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('数据添加失败,原因是:" + ex.Message + "');</script>");
}
}
就是数据是一行一行的
每一行都是绑定数据库的 最后一个按键是linkbutton名字为分配 实质上就是在每行有插入指定列的value的功能。
第一列是序号是自动递增的
然而现在我就是不明白 点某行的linkbutton就是插入的对应这行的值 这是怎么做到的呢!~!~
有源码可以提供下吗? 谢谢了
{
LinkButton myB = (LinkButton)sender;
int myIndex = ((GridViewRow)myB.NamingContainer).RowIndex;//获得行号
int myId = Convert.ToInt16(GridView1.DataKeys[myIndex].Value);//获得id }
这个怎么报索引超出范围。必须为非负值并小于集合大小
参数名:index我的序号是从1开始一直往下递增的啊!~!~
应该怎样修改上面的代码呢
{
LinkButton myB = (LinkButton)sender;
int myIndex = ((GridViewRow)myB.NamingContainer).RowIndex;//获得行号
int myId = Convert.ToInt16(GridView1.DataKeys[myIndex].Value);//获得id }
索引超出范围。必须为非负值并小于集合大小。
参数名:index
报这个错我的序号ID是从1开始递增的啊
该怎样修改上面的代码呢~!~!
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" OnCommand="LinkButton1_Click" CommandArgument='<%#Eval("ID") %>' CommandName="" Text="分配"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>后台: protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton myB = (LinkButton)sender;
int myIndex = ((GridViewRow)myB.NamingContainer).RowIndex;
int myId = Convert.ToInt16(GridView1.DataKeys[myIndex].Value);
this.Response.Write("<script>window.open('SelectMenu.aspx','','height=300,width=300,toolbar=no,menubar=no,scrollbars=no,location=no,status=no');</script>");
}索引超出范围。必须为非负值并小于集合大小。
参数名:index
报这个错多谢老师啊!~!~
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" OnCommand="LinkButton1_Command" CommandArgument='<%#Eval("ID") %>' Text="分配"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>public void LinkButton1_Command(object sender,CommandEventArgs e)
{
//获取该行记录的ID
int myId = Convert.ToInt16(e.CommandArgument.ToString.Trim());
try
{
using(SqlConnection con = new SqlConnection("数据库连接字符串"))
{
con.Open();
string val = this.DropDownList1.SelectedItem.Text.Trim(); //获取下拉列表框中选中的值
string strSQL = "update 表名 set 字段名='"+val+"' where ID="+myId ;
using(SqlCommand cmd = new SqlCommand(strSQL,con))
{
cmd.ExecuteNonQuery();
ClientScript.RegisterClientScriptBlock(GetType(),"","<script>alert('数据修改成功!');</script>");
}
}
}
catch(Exception ex)
{
ClientScript.RegisterClientScriptBlock(GetType(),"","<script>alert('数据修改失败,原因是:"+ex.Message+"');</script>");
}
}
下面那个查数据的在弹出的小窗口里面button的onclick事件里面的啊
linkbutton基本是用来选定行的啊