C# 如何从文本文件中选择特定文本并输入文本框

C# 如何从文本文件中选择特定文本并输入文本框,c#,C#,大家好 我在文本文件中有一个sql脚本,如下所示 create view [dbo].[budget_change-22] as select projectname, projectnumber,location, client FROM OPENROWSET('SQLNCLI', 'SERVER=AABB1089\abcWORKSS_STO;UID=abcworkss;PWD=abcdef, 'SET NOCOUNT ON;SET FM

大家好 我在文本文件中有一个sql脚本,如下所示

create view [dbo].[budget_change-22]
as select projectname, projectnumber,location, client
FROM      OPENROWSET('SQLNCLI', 'SERVER=AABB1089\abcWORKSS_STO;UID=abcworkss;PWD=abcdef, 
                      'SET NOCOUNT ON;SET FMTONLY OFF;EXEC abcworks_sto..SP_Budget_444 38') AS Workchanged_444    
Go

现在,在上面的脚本中,我必须选择服务器值AABB1089\abcWORKSS_STO、UID值ABCWork、Pwd值ABCDef,以便我可以在文本框中替换它们并编辑它们,以创建具有不同名称的新文本文件。

这取决于用户界面使用的框架,但是,当您标记了c时,我将采用winforms

这样做是否可行?将SQL放在一个文件中,但不要填充参数,而是放在可以在代码中使用的占位符中,以添加来自文本框的值,例如:

create view [dbo].[budget_change-22] 
as select projectname, projectnumber,location, client 
FROM OPENROWSET('SQLNCLI', 'SERVER={0};UID={1};PWD={2},  
'SET NOCOUNT ON;SET FMTONLY OFF;EXEC abcworks_sto..SP_Budget_444 38') AS Workchanged_444     
Go
注意文件中读取的代码中的“{x}”,必要时使用string.Format替换{x}值。Psuedo代码:

// Read in the template file
string fileSql = System.File.IO.ReadAllText(pathToYourTemplateFile)

// Replace the place holders with your values (sample assumes
// they are coming from textboxes on the UI
string formattedSql = string.Format(fileSql,
    txtServerName.Text,
    txtUid.Text,
    txtPassword.Text);

// Write out the new file
System.IO.File.WriteAllText(formattedSql, pathToYourNewFile);
如果出于任何原因,您不能使用模板文件,那么您可以使用string.Replace

// Read in the template file
string fileSql = System.File.IO.ReadAllText(pathToYourTemplateFile)

// Replace the values with the input from the UI
fileSql.Replace("AABB1089\abcWORKSS_STO", txtServer.Text)
// ... same for uid ...
// ... same for pwd ...

// Write out the new file
System.IO.File.WriteAllText(fileSql , pathToYourNewFile);