在c#中直接将datatable转换为int

时间:2014-07-11 18:52:11

标签: c# sql datatable command int

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


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

这是我的代码:

班级mydata

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;

1 个答案:

答案 0 :(得分:0)

听起来你需要在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;
                }
            }
        }
    }
}