Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在asp.net中使用c#更新sql server数据库_C#_Asp.net_Sql Server - Fatal编程技术网

在asp.net中使用c#更新sql server数据库

在asp.net中使用c#更新sql server数据库,c#,asp.net,sql-server,C#,Asp.net,Sql Server,没有任何编译错误,但数据库根本没有更新。代码有什么问题 protected void Page_Load(object sender, EventArgs e) { rno.Text = Request.QueryString["rno"];//rno is a textbox string connectionString = @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = db1; Integrated

没有任何编译错误,但数据库根本没有更新。代码有什么问题

protected void Page_Load(object sender, EventArgs e) {
    rno.Text = Request.QueryString["rno"];//rno is a textbox


    string connectionString = @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = db1; Integrated Security = True";
    SqlConnection cnn = new SqlConnection(connectionString);
    cnn.Open();

    String sql = "select fname from table1 where rno = @rno";

    SqlCommand command = new SqlCommand(sql, cnn);
    command.Parameters.AddWithValue("@rno", rno.Text.Trim());
    SqlDataReader reader = command.ExecuteReader();
    if (reader.Read()) {
        fname.Text = reader["xcountry"].ToString().Trim(); //fname is a textbox
    }
    reader.Close();
    command.Dispose();
    cnn.Close();

    fName.ReadOnly = true;
}

protected void modify_Click(object sender, EventArgs e) {

    fName.ReadOnly = false;
}

protected void savechanges_Click(object sender, EventArgs e) {
    string connectionString = @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = db1; Integrated Security = True";
    SqlConnection cnn = new SqlConnection(connectionString);
    cnn.Open();


    String sql = "update table1 set fname=@fname where rno = @rno";

    SqlCommand command = new SqlCommand(sql, cnn);
    command.Parameters.AddWithValue("@fname", sfname);
    command.Parameters.AddWithValue("@rno", rno.Text.Trim());

    command.ExecuteNonQuery();
    command.Dispose();
    cnn.Close();
    fName.ReadOnly = true;
}

我已经试过你的代码,它执行得很好,也更新了数据库表

我试过如下方法:

        string connectionString = @"data source=MS-KIRON-01;initial catalog=TestDatabase;integrated security=True;MultipleActiveResultSets=True";
        SqlConnection cnn = new SqlConnection(connectionString);
        cnn.Open();


        String sql = "update TestTable set fname=@fname where rno =rno";

        SqlCommand command = new SqlCommand(sql, cnn);
        command.Parameters.AddWithValue("@fname", "Test");
        command.Parameters.AddWithValue("@rno", "rno");

        command.ExecuteNonQuery();
        command.Dispose();
        cnn.Close();
我试过的另一种方法

            using (SqlConnection connection = new SqlConnection(connectionString ))
                    {
                        connection.Open();
                        var queryText = "UPDATE TestTable SET fname = '" + requestPram.fname + "' WHERE rno ='" + requestPram.rno + "'";

                        using (SqlCommand cmd = new SqlCommand(queryText, connection))
                        {
                            responseResults = await cmd.ExecuteNonQueryAsync();
                        }
                        connection.Close();

                    }

希望它能有所帮助

在搜索了一段时间后,我发现这段代码执行得非常完美。唯一的问题是,所有内容都在page_Load()方法中,因此每次我更新数据库时页面都会重新加载,从而删除编辑文本框的小窗口。适当的解决方案是将此代码与某个按钮事件相关联,而不是与page_Load()事件相关联。

这里有些东西只有您可以测试。1)
savechanges\u单击
是否会被调用?2) SQL是否使用正确的值正确生成?3)当您直接对数据库运行生成的SQL时会发生什么?您调试过了吗?rno文本的值是多少?。它是否存在于表中的rno列中?下面返回一个整数:int rowsUpdated=command.ExecuteNonQuery();如果返回的数字为零,则表示行不在数据库中,您需要执行插入而不是更新。SQL数据库不会更新不存在的行,也不会插入确实存在的行。调试、断点、调试、断点……。@stuartd我在这里尝试了这些方法。。。1) 保存更改\u单击将被调用。2) 当我在sql查询上运行数据库时,它会得到更新。