将表单数据发送到数据库

时间:2016-02-18 01:12:21

标签: c# sql asp.net visual-studio

我想从表单收集用户输入并将其发送到数据库。我不知道如何告诉sqlCmd.Parameters.Add在函数顶部使用x,y和结果整数。

public void UploadDB_Click(object sender, EventArgs e)
    {
        int x = ConvertX(sender);
        int y = ConvertY(sender);
        int result = ConvertResult(sender);

        SqlConnection sqlCon = new SqlConnection();
        sqlCon.ConnectionString = @"Data Source=(LocalDb)\v11.0;AttachDbFilename=E:\Dropbox\GridViewSandbox-20160215081718.mdf;Initial Catalog=aspnet-GridViewSandbox-20160215081718;Integrated Security=True";

        SqlCommand sqlCmd = new SqlCommand("Procudure1", sqlCon); //can't spell typo :|
        sqlCmd.CommandType = CommandType.StoredProcedure; 
        sqlCmd.Parameters.Add(new SqlParameter(@"x", SqlDbType.Int)); //the x int should go in here somehow? 
        sqlCmd.Parameters.Add(new SqlParameter(@"y", SqlDbType.Int)); //the y int
        sqlCmd.Parameters.Add(new SqlParameter(@"result", SqlDbType.Int)); //the result int

        sqlCon.Open();
        sqlCmd.ExecuteNonQuery();
        sqlCon.Close();
    }

2 个答案:

答案 0 :(得分:1)

它能解决您的问题吗?

sqlCmd.Parameters.Add(new SqlParameter(@"x", SqlDbType.Int) {Value=x}); 

答案 1 :(得分:1)

试试这个:

sqlCmd.Parameters.Add("@x", SqlDbType.Int).Value = x;