|
If you want to write a Driver for an USB dongle, bad news for you: This would probably be hard to achieve in C# because you'd need to write a driver.
Drivers are usually in a somewhat native code written:
Example 1[^]
Example 2[^]
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
i have created a project, to track the daily task done. I used C# and SQL Database for this. (Client server model, more than 50 users are there.)
Now i want to implement a new functionality in my front end tool.
i want to view the data i have entred in the Database for a particular peroid of time. FromDate & ToDate.
i want to export the above from SQL Database to Excel.
I have used Excel Library to create a excel.
Query 1.
how an individual user can view the data he/ She entred in the Database for a particular peroid of time. FromDate & ToDate.
Query 2:
How to i export the above selected data to excel from SQL Database. (am placing a button in the fromend tool "Download")
|
|
|
|
|
1)
SELECT * FROM MyTable WHERE MyDateColumn BETWEEN '2014-01-01' AND '2014-01-31'
2)
Export DataTable to Excel with Formatting in C#[^]
[edit] forgot the FROM clause... Thanks Shameel - OriginalGriff[/edit]
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 – ∞)
modified 7-Feb-14 5:54am.
|
|
|
|
|
OriginalGriff wrote: SELECT * WHERE MyDateColumn BETWEEN '2014-01-01' AND '2014-01-31'
You're sure you aren't missing something?
|
|
|
|
|
Insufficient caffeine, I suspect...
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 – ∞)
|
|
|
|
|
Happens all the time , have some ![Java | [Coffee]](https://codeproject.global.ssl.fastly.net/script/Forums/Images/coffee.gif)
|
|
|
|
|
Thanks for your reply.
In the above query, User will be giving the To Date and From Date during Runtime.
I will be including a caldender for that in my GUI.
So i think we need to fetch the user inputs(Both From & To Dates) in a variable. and use it in our query Statement.
Am confused how to do that. !
Your suggestions please.
|
|
|
|
|
Just pass the DateTime values as parameters to SQL. If you use DateTimePickers, there is no conversion needed:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT iD, description FROM myTable WHERE enterDate BETWEEN @FD AND @TD", con))
{
cmd.Parameters.AddWithValue("@FD", fromDatePicker.Value);
cmd.Parameters.AddWithValue("@TD", toDatePicker.Value);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int id = (int) reader["iD"];
string desc = (string) reader["description"];
Console.WriteLine("ID: {0}\n {1}", iD, desc);
}
}
}
}
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 – ∞)
|
|
|
|
|
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
|
|
|
|
|
|