【问题标题】:Send form data to database将表单数据发送到数据库
【发布时间】:2016-02-18 01:12:20
【问题描述】:

我想从表单中收集用户输入并将其发送到数据库。我不知道如何告诉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();
    }

【问题讨论】:

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


    【解决方案1】:

    它能解决您的问题吗?

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

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

      • 我在这里得到一个 invalidCastException。
      猜你喜欢
      • 2014-01-20
      • 1970-01-01
      • 2021-08-21
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2014-02-24
      相关资源
      最近更新 更多