首先,我将使用什么数据类型将richtextBox 存储到数据库中?变量二进制?
我如何将richtextBox 加载并保存到数据库中?
这就是我使用 nvarchar 数据类型保存数据的方式:
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand myCmd = new SqlCommand("Insert into Employee(Lname, Fname, Mname) Values(@Lname, @Fname, @Mname)", myDatabaseConnection))
{
myCmd.Parameters.AddWithValue("@Fname", FNametextBox.Text);
myCmd.Parameters.AddWithValue("@Lname", LNametextBox.Text);
myCmd.Parameters.AddWithValue("@Mname", MNametextBox.Text);
myCmd.ExecuteNonQuery();
}
}
加载数据:
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand mySqlCommand = new SqlCommand("select Lname, Fname, Mname from Employee", myDatabaseConnection))
using (SqlDataReader sqlreader = mySqlCommand.ExecuteReader())
{
if (sqlreader.Read())
{
LNametextBox.Text = sqlreader["Lname"].ToString();
FNametextBox.Text = sqlreader["Fname"].ToString();
MNametextBox.Text = sqlreader["Mname"].ToString();
}
}