【问题标题】:Converting datatable to int directly in c#在c#中直接将数据表转换为int
【发布时间】:2014-07-11 18:52:10
【问题描述】:

我尝试将只有一个字段(该字段的数据是主键)的数据表转换为 int ,以便在 SQL 命令中使用 Select 等。 但它失败了! 当我将它转换为对象或先将其转换为字符串时,命令出错了! 请帮帮我


我想从具有外键的表中选择 *,其中外代码等于 int 值,该值已从另一个 sql 命令中的表中选择并作为只有一个字段的数据表行返回。

这是我的代码:

类我的数据

public string strsql;
        public DataTable showData()
        {
            SqlConnection Con1 = new SqlConnection("Data Source=.;database=daneshgah;integrated security=true");
            Con1.Open();
            SqlDataAdapter da = new SqlDataAdapter(strsql, Con1);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Con1.Close();
            return (dt);

        }

按钮事件:

    myData search = new myData();
                int aa = int.Parse(txt_stdcourse.Text);
                search.strsql = "select tchNo from University where couNo='" + aa + "'";
                DataTable a = search.showData();
                 string b = a.Rows[0][0].ToString();
                    int c = int.Parse(b);
                    myData akhz = new myData();
                    akhz.strsql = "insert into stc (couNo,tchNo,stuNo)values('" + aa + "','" + c + "','" + id + "')";
                    akhz.Data();
                    lbl_stdcourseok.Visible = false;
                    lbl_stdcourseok.Visible = true;

【问题讨论】:

  • 您的代码似乎有问题。但是,除非我们有 code or information that can reproduce the problem,否则我们无能为力。否则,我们只是在盲目猜测。还请提供有关您遇到的错误的信息。
  • 将一些代码与您尝试过的问题联系起来。此外,您最终是如何在数据表中获得单个值的,可能是您正在使用 DataAdapter 通过数据库加载它。考虑使用ExecuteScalar,它会返回一个项目。
  • 在您的数据库中更改它。
  • 我猜(因为您没有提供代码)您正在尝试将一行或一组行 - 或者可能将整个表转换为 int,请提供代码,或者更好- 使用调试器准确查看您尝试将什么值转换为 int
  • 你在使用SqlParameter吗?

标签: c# sql datatable command int


【解决方案1】:

听起来您需要在 SqlCommand 上使用 ExecuteScalar 而不是使用 DataAdapter。 ExecuteScalar 给出返回的数据集第一行的第一列。

public object RunSQL(string sql)
{
    SqlConnection Con1 = new SqlConnection("Data Source=.;database=daneshgah;integrated security=true");
    Con1.Open();
    SqlCommand command = new SqlCommand(strsql, Con1);
    return command.ExecuteScalar();
}

//In some event handler
int myValue = (int)RunSQL("Select Value from Table where ID = " + ID);

也就是说,请不要那样做 - 这是非常糟糕的做法。您几乎肯定想要创建一个类来对您正在处理的任何数据对象进行建模,而不是从事件处理程序中执行任意 SQL。最好在单独的data access layer 中独立于数据类管理连接。

一个非常基本的例子:

public class Student
{
    public int StudentID { get; set; }
    public bool CurrentlyEnrolled { get; set; }
    public string Name { get; set; }

    public static Student LoadByID(int ID)
    {
        DataTable results = DAL.ExecuteSQL("Select * from Students WHERE StudentID = @StudentID", new SqlParameter("@StudentID", ID));

        if (results.Rows.Count == 1)
        {
            return FillFromRow(results.Rows[0]);
        }
        else
        {
            throw new DataException("Could not find exactly one record with the specified ID.");
        }
    }

    private static Student FillFromRow(DataRow row)
    {
        Student bob = new Student();
        bob.CurrentlyEnrolled = (bool)row["CurrentlyEnrolled"];
        bob.Name = (string)row["Name"];
        bob.StudentID = (int)row["StudentID"];
        return bob;
    }
}

public static class DAL
{
    private const string ConnectionString = "SomeConnectionString"; //Should really be stored in configuration files.

    public static DataTable ExecuteSQL(string SQL, params SqlParameter[] parameters)
    {
        using (SqlConnection connection = new SqlConnection(ConnectionString))
        {
            using (SqlCommand command = new SqlCommand(SQL))
            {
                command.Parameters.AddRange(parameters);

                using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                {
                    DataTable result = new DataTable();
                    adapter.Fill(result);
                    return result;
                }
            }
        }
    }
}

【讨论】:

  • 这是个好主意,但它的类可能会返回其他内容,例如字符串和字符...
  • 好吧,你可以给方法一个对象的返回类型,然后让调用者将它转换为所需的类型,但是有一个本质上是静态的方法,它使用硬编码的连接字符串运行非参数化查询是无论如何,几乎肯定是个坏主意。
  • 查看我修改后的答案 - 我提供了一个示例,以及一个更合适的方法示例。
猜你喜欢
  • 2013-11-06
  • 2019-06-13
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-16
  • 2013-05-02
  • 2016-07-24
相关资源
最近更新 更多