1、关于多项查询,我设了四个查询,
用户查询可以用任何一个条件,点击一个button即可,怎么样写代码?麻烦写出具体的步骤以及整体的代码示例,这是我的部分代码,实在不会,用的是if嵌套,有24种可能,请大家改正。用的是vs2005 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["information"].ToString());
conn.Open();
if (TextBox1.Text == null)
{
if (DropDownList1.Text == null)
{
if (DropDownList2.Text == null)
{
if (TextBox2.Text == null && TextBox3.Text == null)
{
Response.Write("<script>alert('请输入数据!')</script>");
}
if (TextBox2.Text != null && TextBox3.Text != null)
{
string str = "select 项目名 from preprojects where 招商截止时间>='" + TextBox2.Text + "'and 招商截止时间<='" + TextBox3.Text + "'";
SqlDataAdapter da1 = new SqlDataAdapter(str, conn);
DataSet ds1 = new DataSet();
da.Fill(ds1);
GridView1.DataSource = ds1;
GridView1.DataBind();
}
else
{
Response.Write("<script>alert('请输入正确日期!')</script>");
}
}
else
{
if (TextBox2.Text == null && TextBox3.Text == null)
{
string str = "select 项目名 from preprojects where 项目类型='" + DropDownList2.Text + "'";
SqlDataAdapter da2 = new SqlDataAdapter(str, conn);
DataSet ds2 = new DataSet();
da.Fill(ds2);
GridView1.DataSource = ds2;
GridView1.DataBind();
}
if (TextBox2.Text != null && TextBox3.Text != null)
{
string str = "select 项目名 from preprojects where 项目类型='" + DropDownList2.Text + "' and 招商截止时间>='" + TextBox2.Text + "'and 招商截止时间<='" + TextBox3.Text + "'";
SqlDataAdapter da3 = new SqlDataAdapter(str, conn);
DataSet ds3 = new DataSet();
da.Fill(ds3);
GridView1.DataSource = ds3;
GridView1.DataBind();
}
2、向左转|向右转
怎样将gridview1选中的一行插入到数据库中的另一个表中,也是不会,方法特笨,代码如下:if (e.CommandName == "insert")
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["information"].ToString());
conn.Open(); string str = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[0].Text.ToString();
string str1 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[1].Text.ToString();
string str2 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[2].Text.ToString();
string str3 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[3].Text.ToString();
string str4 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[4].Text.ToString();
string str5 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[5].Text.ToString();
string str6 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[6].Text.ToString();
string str7 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[7].Text.ToString();
string str8 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[8].Text.ToString();
string str9 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[9].Text.ToString();
string str10 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[10].Text.ToString();
string str11 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[11].Text.ToString();
Int16 num = Convert.ToInt16(str);
DateTime dt1 = Convert.ToDateTime(str9);
SqlCommand cmd = new SqlCommand("insert into preprojects values (" + num + ",'" + str1 + "',,'" + str2 + "',,'" + str3 + "',,'" + str4 + "','" + str5 + "','" + str6 + "','" + str7 + "','" + str8 + "','" + dt1 + "','" + str10 + "','" + str11 + "')",conn);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('通过')</script>");
conn.Close(); } 最终结果:第一个button怎么点也不出结果,没有任何错误显示。第二个显示“与“int.Parse(string)”最匹配的重载方法具有一些无效参数”。初学者,所以很多不懂,不甚感激! ASP.NETGridViewdataset
用户查询可以用任何一个条件,点击一个button即可,怎么样写代码?麻烦写出具体的步骤以及整体的代码示例,这是我的部分代码,实在不会,用的是if嵌套,有24种可能,请大家改正。用的是vs2005 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["information"].ToString());
conn.Open();
if (TextBox1.Text == null)
{
if (DropDownList1.Text == null)
{
if (DropDownList2.Text == null)
{
if (TextBox2.Text == null && TextBox3.Text == null)
{
Response.Write("<script>alert('请输入数据!')</script>");
}
if (TextBox2.Text != null && TextBox3.Text != null)
{
string str = "select 项目名 from preprojects where 招商截止时间>='" + TextBox2.Text + "'and 招商截止时间<='" + TextBox3.Text + "'";
SqlDataAdapter da1 = new SqlDataAdapter(str, conn);
DataSet ds1 = new DataSet();
da.Fill(ds1);
GridView1.DataSource = ds1;
GridView1.DataBind();
}
else
{
Response.Write("<script>alert('请输入正确日期!')</script>");
}
}
else
{
if (TextBox2.Text == null && TextBox3.Text == null)
{
string str = "select 项目名 from preprojects where 项目类型='" + DropDownList2.Text + "'";
SqlDataAdapter da2 = new SqlDataAdapter(str, conn);
DataSet ds2 = new DataSet();
da.Fill(ds2);
GridView1.DataSource = ds2;
GridView1.DataBind();
}
if (TextBox2.Text != null && TextBox3.Text != null)
{
string str = "select 项目名 from preprojects where 项目类型='" + DropDownList2.Text + "' and 招商截止时间>='" + TextBox2.Text + "'and 招商截止时间<='" + TextBox3.Text + "'";
SqlDataAdapter da3 = new SqlDataAdapter(str, conn);
DataSet ds3 = new DataSet();
da.Fill(ds3);
GridView1.DataSource = ds3;
GridView1.DataBind();
}
2、向左转|向右转
怎样将gridview1选中的一行插入到数据库中的另一个表中,也是不会,方法特笨,代码如下:if (e.CommandName == "insert")
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["information"].ToString());
conn.Open(); string str = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[0].Text.ToString();
string str1 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[1].Text.ToString();
string str2 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[2].Text.ToString();
string str3 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[3].Text.ToString();
string str4 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[4].Text.ToString();
string str5 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[5].Text.ToString();
string str6 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[6].Text.ToString();
string str7 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[7].Text.ToString();
string str8 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[8].Text.ToString();
string str9 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[9].Text.ToString();
string str10 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[10].Text.ToString();
string str11 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[11].Text.ToString();
Int16 num = Convert.ToInt16(str);
DateTime dt1 = Convert.ToDateTime(str9);
SqlCommand cmd = new SqlCommand("insert into preprojects values (" + num + ",'" + str1 + "',,'" + str2 + "',,'" + str3 + "',,'" + str4 + "','" + str5 + "','" + str6 + "','" + str7 + "','" + str8 + "','" + dt1 + "','" + str10 + "','" + str11 + "')",conn);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('通过')</script>");
conn.Close(); } 最终结果:第一个button怎么点也不出结果,没有任何错误显示。第二个显示“与“int.Parse(string)”最匹配的重载方法具有一些无效参数”。初学者,所以很多不懂,不甚感激! ASP.NETGridViewdataset
解决方案 »
- 本人突然需要c#方面的一些书籍,比如什么继承,多态等方面的知识
- asp.net 检测到潜在的危险
- 急急急。。。工具箱没有控件
- 问大家一个问题(母版页)
- [求教]SqlDataSource 中的 SelectCommand 格式问题
- 这两种实现方式的区别?
- 请问在ASP.NET2.0中 怎样判断Application对像是否为空
- 菜鸟问:请问dataset如果在页面中取得输出参数和记录集呀。
- DataGrid中的更新,高手帮我看看。。。
- 有大神做过类似微信的推送服务吗?
- 关于DevExpress 的ASPxGridView一点疑问
- 用户注册信息的时候 先查询用户LoginID是否存在 总是在层与层之间调用方法却取不到值
string sql = "select * from table where 1=1"; if (!string.IsNullOrEmpty(TextBox1.Text))//筛选条件不为空
sql += string.Format(" and 条件1='{0}'", TextBox1.Text);//条件1为表中对应的查询列
if (!string.IsNullOrEmpty(TextBox2.Text))
sql += string.Format(" and 条件2='{0}'", TextBox2.Text);//条件2为表中对应的查询列
2.虽然显得笨,却是基本的,可以改进为参数化查询
{
sql+=" and userid='"+txtuserid.Text+"'";
}
if(txtusername.Text!="")
{
sql+=" and username='"+txtusername.Text+"'";
}执行sql 凑合看吧!自己改进一下