简体   繁体   中英

How to select a certain text from text file and enter into text box

Hi all I have a sql script in text file which is as follows

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

Now in the above script i have to select Server value (AABB1089\\abcWORKSS_STO), UID value(abcwork), Pwd value(abcdef) so that i can replace in text box and edit them to create a new text file with different name.

This depends on the framework your UI is using, however as you have tagged c# I am going to assume winforms.

Would it be feasible to do this instead? Place your SQL in a file but do not populate the parameters, instead put in place holders that you can then use in code to add the values coming in from your textboxes, for example:

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

Notice the '{x}', in your code read in the file and use string.Format to replace {x} values as necessary. Psuedo code:

// 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);

If for whatever reason you can't go down the route of a template file, then you could use string.Replace() for example

// 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM