Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 只给出一个结果的查询_C#_Sql - Fatal编程技术网

C# 只给出一个结果的查询

C# 只给出一个结果的查询,c#,sql,C#,Sql,我的查询只给出一个结果,即一个URL对应一个单词,而它应该针对dt中的每个条目运行。这里的dt是DataTable并且包含许多行,给定的url\u是一个字符串temp是包含要匹配的单词的表。谁能告诉我我的问题出在哪里? wordcount是表的名称url\u String、word和count是列 查询的目的是获取与给定URL和URL匹配的URL 他们在列表中的单词是temp 这是我的搜索和排序功能: public DataTable searchandorder (String sql) {

我的查询只给出一个结果,即一个URL对应一个单词,而它应该针对
dt
中的每个条目运行。这里的
dt
DataTable
并且包含许多行,给定的
url\u
是一个字符串
temp
是包含要匹配的单词的表。谁能告诉我我的问题出在哪里?
wordcount
是表的名称
url\u String
word
count
是列

查询的目的是获取与给定URL和URL匹配的URL 他们在列表中的单词是
temp

这是我的
搜索和排序功能:

public DataTable searchandorder (String sql)
{
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    da.Fill( dt);
    conn.Close();
    Console.Write("table coloumns" + dt.Columns.ToString());
    return dt;
}

您的问题来自于每次循环覆盖
dt1
。您需要合并结果,而不是替换它们,但是如果不看到
db.searchandorder
,我无法说明如何进行合并

此外,您还有一个非常危险的sql注入攻击。如果给定的
url\u是
”;删除表字数--程序将在服务器上执行的查询是什么?您需要与数据适配器一起使用

编辑:那么,现在您已经展示了您的代码,下面是如何修复它的。不是每次通过时都创建一个新的
DataTable
,而是在循环外创建一个,每次都在同一个表中通过<默认情况下,代码>表格适配器在填充表格之前不会清除表格

private YourFunction(DataTable dt)
{
    DataTable dt1 = new DataTable();

    foreach (DataRow row in dt.Rows)
     {
         // read item
         url_given = row["url_String"].ToString();

         String qrystring = "select url_String,word,count from wordcount where url_String='" + url_given + "' and word in (select * from temp) ";
         db.searchandorder(qrystring, dt1);
         // searchandorder is a call to a function that establishes the db connections and passes the query to the data adapter.
     }

    DoSomthingWithResults(dt1);
}

public void searchandorder (String sql, DataTable dt)
{
    conn.Open();

    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
    da.Fill( dt);


    conn.Close();
    Console.Write("table coloumns" + dt.Columns.ToString());

    return dt;
}
现在,这是使您的程序工作的最低要求。你还有很多其他不好的做法

  • 不要重复使用连接:您的
    conn
    对象应该在
    searchandorder
    中创建,并使用
    语句位于
    中。不要担心建立“太多连接”,.NET将使用和重用旧连接

  • 使用
    using
    stations:实现IDisposable的任何内容都应该在
    using
    语句中(除非返回该项)

  • 遵循命名标准:下面有一个C代码,您的类
    searcandhorder
    应该命名为
    SerchAndOrder

  • 使用参数:您容易受到数据适配器使用参数的攻击。它不仅提高了安全性,还允许SQL server缓存查询执行计划,从而使程序更快

下面是应用了这些修复的程序版本

private YourFunction(DataTable dt)
{
    DataTable dt1 = new DataTable();

    foreach (DataRow row in dt.Rows)
     {
         // read item
         url_given = row["url_String"].ToString();
         var parameter = new SqlParameter("@urlGiven", SqlDbType.VarChar, url_given.Length);
         parameter.Value = url_given;

         String qrystring = "select url_String,word,count from wordcount where url_String=@urlGiven and word in (select * from temp) ";
         db.searchandorder(qrystring, dt1, parameter);
         // searchandorder is a call to a function that establishes the db connections and passes the query to the data adapter.
     }

    DoSomthingWithResults(dt1);
}

public void SearchAndOrder (String sql, DataTable dt, params SqlParameter[] parameters)
{
    using(var conn = new SqlConnection(_connectionString))
    using(var da = new SqlDataAdapter(sql, conn))
    {
        da.SelectCommand.Parameters.AddRange(parameters);
        conn.Open();
        da.Fill(dt);
    }

    Console.Write("table coloumns" + dt.Columns.ToString());

    return dt;
}

您的问题来自于每次循环覆盖
dt1
。您需要合并结果,而不是替换它们,但是如果不看到
db.searchandorder
,我无法说明如何进行合并

此外,您还有一个非常危险的sql注入攻击。如果给定的
url\u是
”;删除表字数--程序将在服务器上执行的查询是什么?您需要与数据适配器一起使用

编辑:那么,现在您已经展示了您的代码,下面是如何修复它的。不是每次通过时都创建一个新的
DataTable
,而是在循环外创建一个,每次都在同一个表中通过<默认情况下,代码>表格适配器在填充表格之前不会清除表格

private YourFunction(DataTable dt)
{
    DataTable dt1 = new DataTable();

    foreach (DataRow row in dt.Rows)
     {
         // read item
         url_given = row["url_String"].ToString();

         String qrystring = "select url_String,word,count from wordcount where url_String='" + url_given + "' and word in (select * from temp) ";
         db.searchandorder(qrystring, dt1);
         // searchandorder is a call to a function that establishes the db connections and passes the query to the data adapter.
     }

    DoSomthingWithResults(dt1);
}

public void searchandorder (String sql, DataTable dt)
{
    conn.Open();

    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
    da.Fill( dt);


    conn.Close();
    Console.Write("table coloumns" + dt.Columns.ToString());

    return dt;
}
现在,这是使您的程序工作的最低要求。你还有很多其他不好的做法

  • 不要重复使用连接:您的
    conn
    对象应该在
    searchandorder
    中创建,并使用
    语句位于
    中。不要担心建立“太多连接”,.NET将使用和重用旧连接

  • 使用
    using
    stations:实现IDisposable的任何内容都应该在
    using
    语句中(除非返回该项)

  • 遵循命名标准:下面有一个C代码,您的类
    searcandhorder
    应该命名为
    SerchAndOrder

  • 使用参数:您容易受到数据适配器使用参数的攻击。它不仅提高了安全性,还允许SQL server缓存查询执行计划,从而使程序更快

下面是应用了这些修复的程序版本

private YourFunction(DataTable dt)
{
    DataTable dt1 = new DataTable();

    foreach (DataRow row in dt.Rows)
     {
         // read item
         url_given = row["url_String"].ToString();
         var parameter = new SqlParameter("@urlGiven", SqlDbType.VarChar, url_given.Length);
         parameter.Value = url_given;

         String qrystring = "select url_String,word,count from wordcount where url_String=@urlGiven and word in (select * from temp) ";
         db.searchandorder(qrystring, dt1, parameter);
         // searchandorder is a call to a function that establishes the db connections and passes the query to the data adapter.
     }

    DoSomthingWithResults(dt1);
}

public void SearchAndOrder (String sql, DataTable dt, params SqlParameter[] parameters)
{
    using(var conn = new SqlConnection(_connectionString))
    using(var da = new SqlDataAdapter(sql, conn))
    {
        da.SelectCommand.Parameters.AddRange(parameters);
        conn.Open();
        da.Fill(dt);
    }

    Console.Write("table coloumns" + dt.Columns.ToString());

    return dt;
}

您的问题来自于每次循环覆盖
dt1
。您需要合并结果,而不是替换它们,但是如果不看到
db.searchandorder
,我无法说明如何进行合并

此外,您还有一个非常危险的sql注入攻击。如果给定的
url\u是
”;删除表字数--程序将在服务器上执行的查询是什么?您需要与数据适配器一起使用

编辑:那么,现在您已经展示了您的代码,下面是如何修复它的。不是每次通过时都创建一个新的
DataTable
,而是在循环外创建一个,每次都在同一个表中通过<默认情况下,代码>表格适配器在填充表格之前不会清除表格

private YourFunction(DataTable dt)
{
    DataTable dt1 = new DataTable();

    foreach (DataRow row in dt.Rows)
     {
         // read item
         url_given = row["url_String"].ToString();

         String qrystring = "select url_String,word,count from wordcount where url_String='" + url_given + "' and word in (select * from temp) ";
         db.searchandorder(qrystring, dt1);
         // searchandorder is a call to a function that establishes the db connections and passes the query to the data adapter.
     }

    DoSomthingWithResults(dt1);
}

public void searchandorder (String sql, DataTable dt)
{
    conn.Open();

    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
    da.Fill( dt);


    conn.Close();
    Console.Write("table coloumns" + dt.Columns.ToString());

    return dt;
}
现在,这是使您的程序工作的最低要求。你还有很多其他不好的做法

  • 不要重复使用连接:您的
    conn
    对象应该在
    searchandorder
    中创建,并使用
    语句位于
    中。不要担心建立“太多连接”,.NET将使用和重用旧连接

  • 使用
    using
    stations:实现IDisposable的任何内容都应该在
    using
    语句中(除非返回该项)

  • 遵循命名标准:下面有一个C代码,您的类
    searcandhorder
    应该命名为
    SerchAndOrder

  • 使用参数:您容易受到数据适配器使用参数的攻击。它不仅提高了安全性,还允许SQL server缓存查询执行计划,从而使程序更快

下面是应用了这些修复的程序版本

private YourFunction(DataTable dt)
{
    DataTable dt1 = new DataTable();

    foreach (DataRow row in dt.Rows)
     {
         // read item
         url_given = row["url_String"].ToString();
         var parameter = new SqlParameter("@urlGiven", SqlDbType.VarChar, url_given.Length);
         parameter.Value = url_given;

         String qrystring = "select url_String,word,count from wordcount where url_String=@urlGiven and word in (select * from temp) ";
         db.searchandorder(qrystring, dt1, parameter);
         // searchandorder is a call to a function that establishes the db connections and passes the query to the data adapter.
     }

    DoSomthingWithResults(dt1);
}

public void SearchAndOrder (String sql, DataTable dt, params SqlParameter[] parameters)
{
    using(var conn = new SqlConnection(_connectionString))
    using(var da = new SqlDataAdapter(sql, conn))
    {
        da.SelectCommand.Parameters.AddRange(parameters);
        conn.Open();
        da.Fill(dt);
    }

    Console.Write("table coloumns" + dt.Columns.ToString());

    return dt;
}

你的问题来自于