|
Great, Thank you.
Will implement it in my script.
Cheers,
|
|
|
|
|
You're welcome!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
In a nut shell use a SP to perform this for you. Below is an example script that will create a table, Populate with so test data and a basic SP. Also note that SQL plays funny with the dates in regards WHERE and BETWEEN clauses.
Your BCP may be different from mine but it should be about that folder structure somewhere depending on the version of SQL that you have.
CREATE TABLE DBO.MyData
(
[ID] INT NOT NULL IDENTITY(1,1), --ALL TABLES SHOULD HAVE A ID EITHER SURRAGATE OR NATURAL.
[User] INT NOT NULL, --ASSUME FK CONSTRINT WITH YOU USER TABLE
[Data] VARCHAR(256) NULL,
[DateTimeStamp] DATETIME NOT NULL DEFAULT(GETDATE()), --TIME STAMP FOR THE ENTRY
)
GO
ALTER TABLE MyData ADD PRIMARY KEY (ID)
GO
INSERT INTO DBO.MyData([User], [Data]) VALUES (1,'MESSAGE 1 FROM USER 1')
INSERT INTO DBO.MyData([User], [Data]) VALUES (1,'MESSAGE 2 FROM USER 1')
INSERT INTO DBO.MyData([User], [Data]) VALUES (1,'MESSAGE 3 FROM USER 1')
INSERT INTO DBO.MyData([User], [Data]) VALUES (2,'MESSAGE 1 FROM USER 2')
INSERT INTO DBO.MyData([User], [Data]) VALUES (2,'MESSAGE 2 FROM USER 2')
INSERT INTO DBO.MyData([User], [Data]) VALUES (3,'MESSAGE 1 FROM USER 3')
INSERT INTO DBO.MyData([User], [Data]) VALUES (1,'MESSAGE 4 FROM USER 1')
GO
CREATE PROCEDURE GetData
(
@User INT = NULL,
@FROM DATETIME = NULL,
@TO DATETIME = NULL
)
AS
SELECT
D.[ID],
D.[DATA],
D.[DateTimeStamp]
FROM
DBO.MyData AS D
WHERE
D.[User] = ISNULL(@User, D.[User])
AND
D.[DateTimeStamp] >= ISNULL(@FROM, D.[User])
AND
D.[DateTimeStamp] <= ISNULL(@TO, D.[User])
ORDER BY D.[DateTimeStamp] DESC
GO
Below is an example C# application to consume this along with an example to use BCP for the Export that uses the same SP.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
int _user = 1;
DateTime _from = DateTime.Now.Add(new TimeSpan(-1, 0, 0, 0));
DateTime _to = DateTime.Now.Add(new TimeSpan(1, 0, 0, 0));
string dbServer = "localhost";
string dbDatabase = "";
string dbStoredProcedure = "[dbo].[GetData]";
string outPutFile = @"C:\Temp\MyData.txt";
string bcpPath = @"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\";
using (var conn = new SqlConnection(
string.Format("Server={0};Database={1};Trusted_Connection=True;",
dbServer,
dbDatabase))
)
{
using (var command = conn.CreateCommand())
{
command.CommandText = dbStoredProcedure;
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("User", _user));
command.Parameters.Add(new SqlParameter("From", _from));
command.Parameters.Add(new SqlParameter("To", _to));
conn.Open();
using (var r = command.ExecuteReader())
{
while (r.Read())
{
Console.WriteLine(r[0].ToString());
Console.WriteLine(r[1].ToString());
Console.WriteLine(r[2].ToString());
}
}
conn.Close();
}
}
string bcpArgs = string.Format("[{0}].{1} {2}, '{3}','{4}'",
dbDatabase,
dbStoredProcedure,
_user.ToString(),
_from.ToString("yyyy-MM-dd"),
_to.ToString("yyyy-MM-dd"));
System.Diagnostics.Process.Start(
string.Format("\"{0}BCP.exe\"",
bcpPath),
string.Format("\"{0}\" queryout {1} -c -t, -T -S {2}",
bcpArgs,
outPutFile,
dbServer));
System.Diagnostics.Process.Start(
"Notepad.exe", outPutFile);
}
}
}
The are always better way to do this but I hope that this gives a few pointers. to get better separation of concerns.
PS. You should try to avoid SELECT * as there is a over head and it leave the code open problems in then future.
BCP Ref:
BCP
http://technet.microsoft.com/en-us/library/ms162802.aspx
http://technet.microsoft.com/en-us/library/ms191516.aspx
|
|
|
|
|
I am trying to write code in C# that get mailbox details from user via Power Shell command.
The power shell command script is:
Import-PSSession -session (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http:
Get-Mailbox -Identity helpdesk
The script runs OK from PowerShell.
Now my goal is to run it using C#.
This is my function:
private void button1_Click(object sender, EventArgs e)
{
m_RunSpace = RunspaceFactory.CreateRunspace();
m_RunSpace.Open();
Pipeline pipeLine = m_RunSpace.CreatePipeline();
Command newSession = new Command("New-PSSession");
newSession.Parameters.Add("-ConfigurationName", "Microsoft.Exchange");
newSession.Parameters.Add("-ConnectionUri", "http://myServer.myDomain.com/Powershell");
Command createSessionForExch = new Command("Import-PSSession");
createSessionForExch.Parameters.Add("-Session", newSession);
Command getMailbox = new Command("Get-Mailbox");
getMailbox.Parameters.Add("-Identity", "helpdesk");
pipeLine.Commands.Add(createSessionForExch);
pipeLine.Commands.Add(getMailbox);
Collection<PSObject> commandResults = pipeLine.Invoke();
foreach (PSObject cmdlet in commandResults)
{
}
}
But I receive an error that the "Get-Mailbox" command is not recognize:
http://imageshack.com/a/img593/664/x52q.png
Probably because the Import-PSSessions wasn't invok correctly.
I need help how to run the command Import-PSSession correctly from C#
|
|
|
|
|
|
I am slowly getting tired of RegExes.
RegEx "([\d]{4})-(0?[1-9]|[1][0-2])-([0-2]?[\d]|[3][0-1])[T]?([0-1]?[\d]|[2][0-3])[:]([0-5]?[\d])[:]([0-5]?[\d])\.?([\d]{0,6})?[[Z]|[+-]([\d]|[0][1][0-2][\.]?[\d]?|[\d]{2}?)]?" parses the strings timestamp1, timestamp2 correctly, but not timestamp3:
string stringTimestamp1 = "2014-02-01T14:12:03Z+1";
string stringTimestamp2 = "2014-02-01T14:12:33.2342Z1";
string stringTimestamp3 = "2014-02-01T14:12:33.2342+1";
Undoubtly, the error lies with the "+1" at the end (the Z is desperately left out) and causing the RegEx to fail. It doesn't match.
Why should it? From my POV it should because of [Z]|[+-] (which means either "Z or + or -", doesn't it?).
[Solved, thanks to the help of the resident superbrain, OriginalGriff]
Clean-up crew needed, grammar spill... - Nagy Vilmos
modified 6-Feb-14 10:26am.
|
|
|
|
|
A quick check says it doesn't parse the first one correctly: it matches the stamp up to the Z, then leaves the "+1" as a second match.
Try this:
(\d{4})-(0?[1-9]|1[0-2])-([0-2]?\d|3[0-1])T?([0-1]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)(\.\d{0,6})?Z?([+-]?\d|01[0-2]\.?\d?|\d{2}?)? (I've restructured it a tiny bit, and taken out some of the unnecessary square brackets to make it easier to read)
Expresso matches this as three complete matches, with Z and +n.
One of the things it's worth doing for testing is to break it into its groups:
(\d{4})-
(0?[1-9]|1[0-2])-
([0-2]?\d|3[0-1])
T?([0-1]?\d|2[0-3])
:([0-5]?\d)
:([0-5]?\d)
(\.\d{0,6})?
Z?([+-]?\d|01[0-2]\.?\d?|\d{2}?)? If you look in Expresso on the "Design Mode" tab, and tick the "Ignore White" option it will ignore whitespace in the pattern which makes the groups easier to find.
It still generates a single unbroken line from the Code View ready to paste into your app.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
I think I love you
Now seriously, thank you very much !
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
I may stop answering your questions...
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
|
ROFL! Nice appreciative post.
/ravi
|
|
|
|
|
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
For days when OG will stop answering your question...
I found this visual tool very good to understand problems in RegEx...
http://www.regexper.com/[^]
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
OG has already recommended Expresso[^], but thank you very much anyways.
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
|
Thank you very much for the answer, Peter, but OG's solution seems to work fine for me (and it is shorter).
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
|
Ooops... I overlooked that and posted it here, since I was having this problem in C#.
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
If you heard sounds, like someone muttering "tsk ... tsk ... tsk," it was not I
“But I don't want to go among mad people,” Alice remarked.
“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”
“How do you know I'm mad?” said Alice.
“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll
|
|
|
|
|
hi,
I am trying to SetValue for my registry ket at LocalMachine->Software->MyApplication but getting the following error:
Requested registry access is not allowed.
this is the code I am using:
RegistryKey regkeyLocalMachine = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MyPOSMachine", true);
regkeyLocalMachine.SetValue(value_name, value_data, value_kind);
and the error is thrown on the first line (the OpenSubKey).
Thanks,
Jassim[^]
Technology News @ www.JassimRahma.com
|
|
|
|
|
You probably do not have the required permission to access the registry: write access requires Admin so your app may need elevation in order to work.
If you can, avoid using the registry - it is restricted now (and has been since Vista) and is likely to become more restricted in the future, not less.
If you can store your information elsewhere, then do: I tend to use a GUID app id folder below one of the public app data folders whenever I can: Where should I store my data?[^]
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
but can I use a file and still make it available for all user profile on the same computer or it will be per user and same configuration should be done again for every user on this computer?
Technology News @ www.JassimRahma.com
|
|
|
|
|
Either: Environment.SpecialFolder.CommonApplicationData holds folders that are common to all users on a PC, while Environment.SpecialFolder.ApplicationData is user specific. I frequently use both, with a common GUID app ID folder name in each for the settings data.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
can I ask why GUID? why not the application name? any reason?
Technology News @ www.JassimRahma.com
|
|
|
|
|
Application name may not be unique: a lot of the good ones are used already!
If you duplicate an existing app name, then it's quite likely that it's config info will interfere with yours or vice versa.
GUID's are less likely to be repeated, and harder for users to find and play with! If they don't need to access it manually, then why let them have an easy route?
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|