如何将ExecuteScalar()的结果转换为int?

时间:2017-06-22 13:50:53

标签: c#

我实际上是在尝试使用ExecuteScalar()插入后获取主键。因为它在插入后返回第一行的第一列。但我得到0.我不知道为什么会这样。请帮帮我。

query = "Insert into Admissions(Admission_date, Student_name, Father_name, Mother_name, DOB, Gender, Address, State, City, Pincode, Admission_for, Previous_school, Fees) values ('" + txtAdmDate.Text + "','" + txtStudentName.Text + "','" + txtFatherName.Text + "','" + txtMotherName.Text + "','" + dob + "','" + gender + "','" + txtAddress.Text + "','" + txtState.Text + "','" + txtCity.Text + "','" + txtPincode.Text + "','" + cmbClass.Text + "','" + txtPreviousSchool.Text + "','" + txtFees.Text + "')";

cmd = new SqlCommand(query, con);
con.Open();

int admid = Convert.ToInt32(cmd.ExecuteScalar());

3 个答案:

答案 0 :(得分:0)

尝试在SQL命令中使用OUTPUT子句返回有关命令的信息。

答案 1 :(得分:0)

您的代码/问题存在一些问题。

  1. 您的代码容易受到SQL Injection攻击。您需要参数化查询

  2. 设计的INSERT语句并不意味着返回任何内容,如果要返回刚刚插入的主键,则需要在查询中输出参数(更好的是,存储过程)。

  3. 快速谷歌“在sql insert c#上返回主键”会给你带来大量的结果。您的问题几乎是逐字逐句here。事实上,我的答案基本上是最顶层的答案代码(修改后供您使用)。

  4. 以下是我的回答

    //Create an Admission class that represents your data
    public static int Save(Admission admission)
    {
        var conn = DbConnect.Connection();
        const string sqlString = "Admissions(Admission_date, Student_name, Father_name, Mother_name, DOB, Gender, " +
            "Address,   State, City, Pincode, Admission_for, Previous_school, Fees) values (@AdmissionDate, @StudentName, " + 
            "@FatherName, @MotherName, @DOB, @Gender, @Address, @State, @City, @Pincode, @AdmissionFor, @PreviousSchool, " +
            "@Fees) SELECT SCOPE_IDENTITY()";
    
        using (conn)
        {
            using (var cmd = new SqlCommand(sqlString, conn))
            {
                cmd.Parameters.AddWithValue("@AdmissionDate", admission.AdmissionDate);
                cmd.Parameters.AddWithValue("@StudentName", admission.StudentName);
                cmd.Parameters.AddWithValue("@FatherName", admission.FatherName);
                cmd.Parameters.AddWithValue("@MotherName", admission.MotherName);
                cmd.Parameters.AddWithValue("@DOB", admission.DOB);
                cmd.Parameters.AddWithValue("@Gender", admission.Gender);
                cmd.Parameters.AddWithValue("@Address", admission.Address);
                cmd.Parameters.AddWithValue("@State", admission.State);
                cmd.Parameters.AddWithValue("@City", admission.City);
                cmd.Parameters.AddWithValue("@Pincode", admission.Pincode);
                cmd.Parameters.AddWithValue("@AdmissionFor", admission.AdmissionFor);
                cmd.Parameters.AddWithValue("@PreviousSchool", admission.PreviousSchool);
                cmd.Parameters.AddWithValue("@Fees", admission.Fees);
    
                cmd.CommandType = CommandType.Text;
                conn.Open();
                return (int)(decimal)cmd.ExecuteScalar();
            }
        }
    }
    

答案 2 :(得分:0)

public int NewProperty(PropertyData propertyData)
    {
        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("InsertUpdateProperty", con);

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@id", propertyData.ID);
            cmd.Parameters.AddWithValue("@ListPropertyFor", propertyData.ListPropertyFor);
            cmd.Parameters.AddWithValue("@PropertyTypeId", propertyData.PropertyTypeId);
            cmd.Parameters.AddWithValue("@PropertyLoction", propertyData.PropertyLocation);
            cmd.Parameters.AddWithValue("@Locality", propertyData.Locality);
            cmd.Parameters.AddWithValue("@ProjectName", propertyData.ProjectName);
            cmd.Parameters.AddWithValue("@PropertyDescription", propertyData.PropertyDescription);
            cmd.Parameters.AddWithValue("@SuperBulidupArea", propertyData.SuperBulidupArea);
            cmd.Parameters.AddWithValue("@SuperBulidupId", propertyData.SuperBulidupAreaId);
            cmd.Parameters.AddWithValue("@BulidupArea", propertyData.BulidupArea);
            cmd.Parameters.AddWithValue("@BulidupAreaId", propertyData.BulidupAreaId);
            cmd.Parameters.AddWithValue("@CarpetArea", propertyData.CarpetArea);
            cmd.Parameters.AddWithValue("@CarpetAreaId", propertyData.CarpetAreaId);
            cmd.Parameters.AddWithValue("@Bathrooms", propertyData.Bathrooms);
            cmd.Parameters.AddWithValue("@Bedrooms", propertyData.Bedrooms);
            cmd.Parameters.AddWithValue("@Balconies", propertyData.Balconies);
            cmd.Parameters.AddWithValue("@FurnishedId", propertyData.FurnishedId);
            cmd.Parameters.AddWithValue("@TotalFloors", propertyData.TotalFloors);
            cmd.Parameters.AddWithValue("@PropertyOnFloors", propertyData.PropertyOnFloor);
            cmd.Parameters.AddWithValue("@Parking", propertyData.Parking);
            cmd.Parameters.AddWithValue("@AvalibiltyId", propertyData.AvalibiltyId);
            cmd.Parameters.AddWithValue("@AgeOfProperty", propertyData.AgeOfProperty);
            cmd.Parameters.AddWithValue("@OwnerShip", propertyData.OwenerShip);
            cmd.Parameters.AddWithValue("@Price", propertyData.Price);
            cmd.Parameters.AddWithValue("@IsActive", propertyData.IsActive);

            con.Open();

            int i = Convert.ToInt32(cmd.ExecuteScalar()); 

            con.Close();
            return i;
        }
    }