Hi! I used this code:
object filename = @"F:\Test.docx";
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
doc = app.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);
List<string> data = new List<string>();
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
string temp = doc.Paragraphs[i + 1].Range.Text.Trim();
if (temp != string.Empty)
{
data.Add(temp);
}
}
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Character"), new DataColumn("Number", typeof(int)) });
for (int i = 0; i < data.Count; i++)
{
string txt = data[i].ToUpper();
char chr = txt[0];
int number = (int)chr;
int value = 0;
if (number > 1039)
{
value = (number + 1) - 1040;
}
else
{
value = (number + 1) - 65;
}
dt.Rows.Add((i + 1), txt , value);
}
gvDetails.DataSource = dt;
gvDetails.DataBind();
doc.Close(ref missing, ref missing, ref missing);
it’s worked. I want insert result from datagridview into database.
Id
|
Separate
|
Number
|
1
|
2016_03_30
|
1
|
2
|
2016_03_30
|
2
|
3
|
2016_03_30
|
3
|
4
|
2016_03_30
|
4
|
5
|
2016_03_30
|
5
|
6
|
2016_03_30
|
5
|
7
|
2016_03_30
|
3
|
8
|
2016_03_30
|
1
|
9
|
2016_03_30
|
2
|
10
|
2016_03_30
|
3
|
11
|
2016_03_30
|
4
|
12
|
2016_03_30
|
5
|
If separate exists then update else insert.
private void Form1_Load(object sender, EventArgs e)
{
this.BindDataGridView();
}
private void BindDataGridView()
{
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Separate"), new DataColumn("Number", typeof(int)) });
this.gvDetails.DataSource = dt;
}
private void btnInsert_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in gvDetails.Rows)
{
string constring = @"Data Source=USER-PC;Initial Catalog=dbsUchProc;Integrated Security=true";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "insertkey";
cmd.Parameters.AddWithValue("@Id", row.Cells["Id"].Value);
cmd.Parameters.AddWithValue("@Separate ", row.Cells["Separate"].Value);
cmd.Parameters.AddWithValue("@Number ", row.Cells["Number"].Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
MessageBox.Show("Records inserted.");
dt.Clear();
}
Proc
create procedure insertkey
@Id char(4),
@Separate char(24),
@Number char(1),
as
begin
if exists(select Separate from tblOtv where Separate = @Separate)
begin
update tblOtv
set Id = @Id,
Separate = @Separate,
Number = @Number
where Separate = @Separate
end
else
begin
insert into tblOtv(Id, Separate,Number)values(@Id, @Separate,@Number)
end
end
I used this script but not insert data into database. It’s only add one row into database. Can you help me?