如何使用C#修改* .dat文件中的文本 [英] How to modify text in a *.dat file using c#

查看:384
本文介绍了如何使用C#修改* .dat文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用c#修改* .dat文件.
实际上,SQL脚本是在该* .dat文件中编写的,而我想在c#读取表名称的同时更改表名称.

我已经使用下面的代码来读取* .dat文件.

I want to modify *.dat file using c#.
actually SQL scripts are written in that *.dat file and i want to change the table name while it is read by c#.

i have used this below code to read *.dat file.

System.IO.StreamReader oRead = null;
                    string[] FirstLine = new string[500];
                    int lineNo = 0;
                    oRead = System.IO.File.OpenText(Application.StartupPath + "\\test.dat");
                    lineNo = 0;
                    while (!oRead.EndOfStream)
                    {
                        FirstLine[lineNo] = oRead.ReadLine();
                        lineNo += 1;
                    }
                    for (int i = 0; i <= (lineNo - 1); i++)
                    {
                        cmd = new SqlCommand(FirstLine[i].ToString(), conn);
                        cmd.ExecuteNonQuery();
                    }



在test.dat
我写了这样的脚本:



In the test.dat
i have written script like this:

CREATE TABLE [SectionMaster] (	[Sec_ID] [int] NOT NULL ,	[Sec_Name] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,	[Remarks] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,	CONSTRAINT [PK_SectionMaster] PRIMARY KEY  CLUSTERED 	(		[Sec_ID]	)  ON [PRIMARY] ,	CONSTRAINT [IX_SectionMaster_1] UNIQUE  NONCLUSTERED 	(		[Sec_Name]	)  ON [PRIMARY] ) ON [PRIMARY]


并且我想更改由


and i want to change the table name while it is read by

StreamReader

读取时的表名,并将像

SectionMaster

这样的表名更改为

SectionMaster_test

测试"是单独给出的.
希望我能正确解释我的问题.
任何查询都可以随时询问.

有人可以建议我在阅读脚本时如何修改脚本吗?
在此先感谢.

"test" is given separately.
Hope i can explain my question properly.
any queries don''t hesitate to ask.

can anyone suggest me how to modify script while reading the script?
Thanks in advance.

推荐答案

这就是 ^ ]适用于:
string sectionAppendix = "test";   // wherever this may come from

for (int i = 0; i < lineNo; i++)
{
    string cmdString = FirstLine[i];
    
    cmdString = cmdString.Replace(
        "[PK_SectionMaster]",
        "[PK_SectionMaster_" + sectionAppendix + "]"
    );

    cmd = new SqlCommand(cmdString, conn);
    cmd.ExecuteNonQuery();
}


这篇关于如何使用C#修改* .dat文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆