如何在SQL Server中保存XML数据?

时间:2021-11-10 04:37:20

In my table there is one XML column. I want to fetch the XML data in one textbox and make some corrections and update it:

在我的表中有一个XML列。我想在一个文本框中获取XML数据并进行一些更正并更新它:

private void button2_Click(object sender, EventArgs e)
{
     con.Open();
     string str = "select C1 from TableName where C2='" + txt1.Text+ "'";

     SqlCommand cmd1 = new SqlCommand(str, con);
     XmlReader xml = cmd1.ExecuteXmlReader();
     xml.Read();

     txt2.Text = xml.ReadOuterXml();

     XmlDocument doc = new XmlDocument();
     doc.PreserveWhitespace = true;
     doc.LoadXml(txt2.Text);
}

Now I want to make some changes and update it in my database. When I try to change in textbox it does not work. How can I make changes and update in database? Please help

现在我想进行一些更改并在我的数据库中更新它。当我尝试更改文本框时,它不起作用。如何在数据库中进行更改和更新?请帮忙

1 个解决方案

#1


Update Like this

像这样更新

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{ 
command.CommandText = "Update TableName set  C1 =  @C1 where C2 = @C2 ";
command.Parameters.AddWithValue("@C1", Textbox2.Text);
command.Parameters.AddWithValue("@C2", Textbox1.text);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
} 

#1


Update Like this

像这样更新

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{ 
command.CommandText = "Update TableName set  C1 =  @C1 where C2 = @C2 ";
command.Parameters.AddWithValue("@C1", Textbox2.Text);
command.Parameters.AddWithValue("@C2", Textbox1.text);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}