728x90
반응형
RTF값을 string으로 바꿔서 DB에 넣고, string을 다시 RTF로 바꿔서 UI에 표시해주는걸
해보자.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Data;
using System.Xaml.Schema;
using System.Web;
using System.Net;
using System.Web.Services;
using System.IO;
namespace SkyAndCode_Kyungwon2016
{
public static class RTFHandler
{
public static string GetRTF(FlowDocument flowDocument)
{
using (MemoryStream ms = new MemoryStream())
{
using (StreamReader sr = new StreamReader(ms))
{
TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
range.Save(ms, DataFormats.Rtf);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
string rtf = sr.ReadToEnd();
return rtf;
}
}
}
public static string GetRTF2(FlowDocument flowDocument)
{
TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
return range.Text;
}
public static void LoadRTF(string rtf, FlowDocument flowDocument)
{
using (MemoryStream ms = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(ms))
{
TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
sw.Write(rtf);
sw.Flush();
ms.Seek(0, SeekOrigin.Begin);
range.Load(ms, DataFormats.Rtf);
}
}
}
}
}
|
cs |
먼저 이렇게 클래스를 하나 만들어준다.
RTFHandler 클래스를 가져가서 그대로 쓰면됨.
1
2
3
4
5
|
string rtf = RTFHandler.GetRTF(RichBoxAll.Document);
string rtf2 = RTFHandler.GetRTF2(RichBoxAll.Document);
rtf2 = rtf2.Replace("'", "");
rtf2 = rtf2.Replace(@"""", "");
|
cs |
이 클래스를 사용해서 RichBoxAll이라고 하는 RichTextBox 컨트롤의 내용을 string으로 바꾼다.
rtf 와 rtf2 의 차이는, 하나는 전부를 담는것이고, 하나는 문자만 담는것이다.
rtf자체를 담았을때 검색을 할수가 없다.
그래서 문자만 있는것을 따로 해서 하나더 ,, 그러니까 2개를 DB에 올리는것.
(문자만 담는것은, 나중에 문자를 뭔가를 검색할때 사용한다., 필요없으면 하나만 올리면됨)
아래와 같이, DB에 넣으면 되겠다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
string constr = @"server=localhost\서버명,포트;uid=sa;pwd=비번;database=DB명";
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
if (conn.State == ConnectionState.Closed)
{
conn.Open();
cmd.Connection = conn;
// MessageBox.Show("기존최강" +h1);
cmd.CommandText = "insert into Table_Code_KW (RTFValue,TextValue)" +
"values (@RTF1,@RText1);";
SqlParameter para4 = new SqlParameter("@RTF1", SqlDbType.NVarChar, rtf.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, rtf);
SqlParameter para5 = new SqlParameter("@RText1", SqlDbType.NVarChar, rtf2.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, rtf2);
cmd.Parameters.Add(para4);
cmd.Parameters.Add(para5);
cmd.ExecuteNonQuery();
|
cs |
다시 가져오는 방법은 아래와 같이
LoadRTF 함수를 써서,
가져온 string을 RichMemo_kw1이라는 RichTextBox에 뿌리는데,
위에서 Text로 저장했던것은 나중에 검색용으로만 쓰기때문에, LoadRTF로 불러올수 없다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
private void Button_Click_3(object sender, RoutedEventArgs e)
{
try
{
string constr = @"server=localhost\KWDB2019,포트;uid=sa;pwd=비번;database=DB명";
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd2 = new SqlCommand("select * from Table_Memo_Kw_Stock ", conn);
cmd2.CommandType = CommandType.Text;
cmd2.Connection.ConnectionString = constr;
SqlDataReader reader = null;
if (conn.State == ConnectionState.Closed)
{
conn.Open();
reader = cmd2.ExecuteReader();
reader.Read();
RTFHandler.LoadRTF(reader.GetString(0), this.RichMemo_kw1.Document);
}
}
catch (Exception e0)
{
MessageBox.Show(e0.Message);
}
}
|
cs |
검색하는건 나중에 포스팅하겠다.
728x90
반응형
'다양한 실전소스코드 > WPF(C#)' 카테고리의 다른 글
[Solved] C# WPF DataGrid Selected Chang Error , DataGrid Load Error (0) | 2021.06.28 |
---|---|
[Solved] C# WPF 듀얼모니터 다른 Window에 폼 띄우기 (0) | 2021.06.28 |
[Solved] C# WPF Thread Join 사용방법 (쓰레드 완료까지 대기) (0) | 2021.06.27 |
[Solved] C# WPF Width Height Length Set in code (Xmal 50* 값 입력 방법) (0) | 2021.06.27 |
[Solved] C# WPF MSSQL DB to DataGrid (DB의 값을 테이블에 뿌릴때) (0) | 2021.06.27 |
댓글