我在C#表单上有一个datagridview,它有很多行。选择选项和复选框。
如果我使用复选框选择选择10行,我需要将列选择行值传递给SQL查询以过滤来自另一个表的记录。
以下是我的编码但不起作用我不知道如何解决这个问题。
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=acc;Integrated Security=True");
SqlCommand cmd1 = new SqlCommand(@"select db.date,db.type,db.refno,db.itmcod,db.qty,db.cuscod, db.cstcod,cus.cusnam INTO ##wec from fstktxn as db INNER JOIN fcustomer as cus on db.cuscod = cus.cuscod where itmcod = "dataGridView1.Rows[j].Cells["title"].Value", ", conn1);
conn1.Open();
cmd1.ExecuteNonQuery();
SqlBulkCopy bulkCopy = new SqlBulkCopy(conn1);
bulkCopy.DestinationTableName = "##tmp1";
conn1.Close();
}
有没有使用gridview选择过滤数据的解决方案?
答案 0 :(得分:0)
如果我理解你的要求你应该这样做。
首先获取datagridveiw数据。
DataTable data=datfridviewname.DataSource as DataTable;
con.Open();
SqlBulkCopy sbc = new SqlBulkCopy(con);
sbc.DestinationTableName = @"tableName";
sbc.WriteToServer(data);
答案 1 :(得分:0)
尝试阅读How to use parameterised queries in SQL
它显示了如何安全地将变量添加到您的查询中
答案 2 :(得分:0)
我认为您要问的是“我如何使用java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials
at org.openqa.selenium.remote.HttpCommandExecutor.getDefaultClientFactory(HttpCommandExecutor.java:89)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:63)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:58)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:155)
at com.fieldforce.test.testSample.setUp(testSample.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.apache.http.auth.Credentials
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 24 more
java.lang.NullPointerException
at com.fieldforce.test.testSample.tearDown(testSample.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
中的选定行来形成DataGridView
查询?”。假设这是尝试,你应该把它分成两个步骤:
in
查询in
查询第一步可以通过迭代in
的{{1}},并检查({推荐).Rows
DataGridView
的{{1}}值来完成;如果选中:将过滤器值添加到某个列表中。例如:
.Checked
第二步更复杂,除非你使用像“dapper”这样的自动化工具。如果你 使用“dapper”,你可以使用:
DataGridViewCheckBoxColumn
和就是它。如果没有,您需要手动编写命令,例如:
List<string> titles = new List<string>();
foreach (GridViewRow row in dataGridView1.Rows)
{
if(row.Cells["CheckBoxColumnName"].Value == true)
{
titles.Add((string)row.Cells["TitleColumnName"].Value);
}
}
然后像往常一样执行命令。您可能也需要考虑零情况;应该是所有行吗?没有行?
答案 3 :(得分:-1)
如果您尝试获取所选的复选框行,那么使用它可以获得它
protected void button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in dataGridView1.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
if (chk != null & chk.Checked)
{
SqlConnection conn1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=acc;Integrated Security=True");
SqlCommand cmd1 = new SqlCommand(@"select db.date,db.type,db.refno,db.itmcod,db.qty,db.cuscod, db.cstcod,cus.cusnam INTO ##wec from fstktxn as db INNER JOIN fcustomer as cus on db.cuscod = cus.cuscod where itmcod = "dataGridView1.Cells["title"].Value", ", conn1);
conn1.Open();
cmd1.ExecuteNonQuery();
SqlBulkCopy bulkCopy = new SqlBulkCopy(conn1);
bulkCopy.DestinationTableName = "##tmp1";
conn1.Close();//Do something
}
}
}