29-06-21 09:34 AM
Private moConnection As SqlConnection
Private moTransaction As SqlTransaction
Private Sub WriteCsv(ByVal sql As String, ByVal out As TextWriter)
' The target data set
Dim ds As New DataSet()
' Build and execute the command, dumping the data into the above set
Using cmd As New SqlCommand()
cmd.Connection = moConnection
cmd.Transaction = moTransaction
cmd.CommandText = sql
Using da As New SqlDataAdapter()
da.SelectCommand = cmd
da.Fill(ds)
End Using
End Using
' Go through the data, escaping and outputting to the writer
Dim quotesRequiredChars() As Char = {","c, """"c}
For Each tab As DataTable In ds.Tables
' Column headers first
Dim first As Boolean = True
For Each col As DataColumn In tab.Columns
If first Then first = False Else out.Write(",")
Dim val As String = col.ColumnName
If val.IndexOfAny(quotesRequiredChars) >= 0 Then _
val = """" & val.Replace("""", """""") & """"
out.Write(val)
Next
out.WriteLine()
' Then a line for each row
For Each row As DataRow in tab.Rows
first = True
For Each col As DataColumn in tab.Columns
If first Then first = False Else out.Write(",")
If row.IsNull(col) Then Continue For
Dim val As String = row(col).ToString()
If val.IndexOfAny(quotesRequiredChars) >= 0 Then _
val = """" & val.Replace("""", """""") & """"
out.Write(val)
Next
out.WriteLine()
Next
' Separate each table with a couple of blank lines
out.WriteLine()
out.WriteLine()
Next
out.Flush()
End Sub
Private Function GetScalar(Of T)(ByVal sql As String, _
ByRef success As Boolean, ByRef msg As String) As T
' Assume Success
success = True
msg = ""
Try
Using cmd As New SqlCommand()
cmd.Connection = moConnection
cmd.CommandText = SQL
cmd.Transaction = moTransaction
Return CType(cmd.ExecuteScalar(), T)
End Using
Catch ex As Exception
success = False
msg = ex.Message
Return Nothing
End Try
End Function
29-06-21 03:46 PM
SqlCommand sql_cmnd = new SqlCommand("PROC_NAME", sqlCon);
sql_cmnd.CommandType = CommandType.StoredProcedure;
sql_cmnd.Parameters.AddWithValue("@FIRST_NAME", SqlDbType.NVarChar).Value=firstName;
sql_cmnd.Parameters.AddWithValue("@LAST_NAME", SqlDbType.NVarChar).Value=lastName;
sql_cmnd.Parameters.AddWithValue("@AGE", SqlDbType.Int).Value = age;
sql_cmnd.ExecuteNonQuery();
30-06-21 06:32 AM
13-08-21 10:01 AM
13-08-21 11:52 AM
SqlCommand sql_cmnd = new SqlCommand("PROC_NAME", sqlCon);
sql_cmnd.CommandType = CommandType.StoredProcedure;
sql_cmnd.Parameters.AddWithValue("@FIRST_NAME", SqlDbType.NVarChar).Value=firstName;
sql_cmnd.Parameters.AddWithValue("@LAST_NAME", SqlDbType.NVarChar).Value=lastName;
sql_cmnd.Parameters.AddWithValue("@AGE", SqlDbType.Int).Value = age;
// Define the return parameter.
var returnParameter = sql_cmnd.Parameters.Add("@ReturnVal", SqlDbType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
sql_cmnd.ExecuteNonQuery();
Cheers,
16-08-21 07:51 AM