Guest User

Untitled

a guest
Jan 18th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. sqlcmd.exe -S .PDATA_SQLEXPRESS -U sa -P 2BeChanged! -d PDATA_SQLEXPRESS -s ; -W -w 100 -Q "SELECT tPatCulIntPatIDPk, tPatSFirstname, tPatSName, tPatDBirthday FROM [dbo].[TPatientRaw] WHERE tPatSName = '%name%' "
  2.  
  3. string queryString = "SELECT tPatCulIntPatIDPk, tPatSFirstname, tPatSName, tPatDBirthday FROM [dbo].[TPatientRaw] WHERE tPatSName = @tPatSName";
  4. string connectionString = "Server=.PDATA_SQLEXPRESS;Database=;User Id=sa;Password=2BeChanged!;";
  5.  
  6. using (SqlConnection connection = new SqlConnection(connectionString))
  7. {
  8. SqlCommand command = new SqlCommand(queryString, connection);
  9. command.Parameters.AddWithValue("@tPatSName", "Your-Parm-Value");
  10. connection.Open();
  11. SqlDataReader reader = command.ExecuteReader();
  12. try
  13. {
  14. while (reader.Read())
  15. {
  16. Console.WriteLine(String.Format("{0}, {1}",
  17. reader["tPatCulIntPatIDPk"], reader["tPatSFirstname"]));// etc
  18. }
  19. }
  20. finally
  21. {
  22. // Always call Close when done reading.
  23. reader.Close();
  24. }
  25. }
  26.  
  27. // sqlcmd.exe
  28. // -S .PDATA_SQLEXPRESS
  29. // -U sa
  30. // -P 2BeChanged!
  31. // -d PDATA_SQLEXPRESS
  32. // -s ; -W -w 100
  33. // -Q "SELECT tPatCulIntPatIDPk, tPatSFirstname, tPatSName, tPatDBirthday FROM [dbo].[TPatientRaw] WHERE tPatSName = '%name%' "
  34.  
  35. DataTable dt = new DataTable() ;
  36. int rows_returned ;
  37.  
  38. const string credentials = @"Server=(localdb).PDATA_SQLEXPRESS;Database=PDATA_SQLEXPRESS;User ID=sa;Password=2BeChanged!;" ;
  39. const string sqlQuery = @"
  40. select tPatCulIntPatIDPk ,
  41. tPatSFirstname ,
  42. tPatSName ,
  43. tPatDBirthday
  44. from dbo.TPatientRaw
  45. where tPatSName = @patientSurname
  46. " ;
  47.  
  48. using ( SqlConnection connection = new SqlConnection(credentials) )
  49. using ( SqlCommand cmd = connection.CreateCommand() )
  50. using ( SqlDataAdapter sda = new SqlDataAdapter( cmd ) )
  51. {
  52. cmd.CommandText = sqlQuery ;
  53. cmd.CommandType = CommandType.Text ;
  54. connection.Open() ;
  55. rows_returned = sda.Fill(dt) ;
  56. connection.Close() ;
  57. }
  58.  
  59. if ( dt.Rows.Count == 0 )
  60. {
  61. // query returned no rows
  62. }
  63. else
  64. {
  65.  
  66. //write semicolon-delimited header
  67. string[] columnNames = dt.Columns
  68. .Cast<DataColumn>()
  69. .Select( c => c.ColumnName )
  70. .ToArray()
  71. ;
  72. string header = string.Join("," , columnNames) ;
  73. Console.WriteLine(header) ;
  74.  
  75. // write each row
  76. foreach ( DataRow dr in dt.Rows )
  77. {
  78.  
  79. // get each rows columns as a string (casting null into the nil (empty) string
  80. string[] values = new string[dt.Columns.Count];
  81. for ( int i = 0 ; i < dt.Columns.Count ; ++i )
  82. {
  83. values[i] = ((string) dr[i]) ?? "" ; // we'll treat nulls as the nil string for the nonce
  84. }
  85.  
  86. // construct the string to be dumped, quoting each value and doubling any embedded quotes.
  87. string data = string.Join( ";" , values.Select( s => """+s.Replace(""","""")+""") ) ;
  88. Console.WriteLine(values);
  89.  
  90. }
  91.  
  92. }
  93.  
  94. string userInput = "Brian";
  95. var process = new Process();
  96. var startInfo = new ProcessStartInfo();
  97. startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  98. startInfo.FileName = "cmd.exe";
  99. startInfo.Arguments = string.Format(@"sqlcmd.exe -S .PDATA_SQLEXPRESS -U sa -P 2BeChanged! -d PDATA_SQLEXPRESS
  100. -s ; -W -w 100 -Q "" SELECT tPatCulIntPatIDPk, tPatSFirstname, tPatSName,
  101. tPatDBirthday FROM [dbo].[TPatientRaw] WHERE tPatSName = '{0}' """, userInput);
  102.  
  103. process.StartInfo = startInfo;
  104. process.Start();
Add Comment
Please, Sign In to add comment