parse date from sqlserver to c# - c#

I need some guidance for parsing dates. My database table contains values
ID (int) and date (datetime: yyyy-mm-dd HH:mm:ss.fff format) and status
example
1000 & 2014-02-18 20:32:20.657 & 1
2000 & 2014-02-18 20:32:20.658 & 1
3000 & NULL & -1
I have a C# program that looks at this table for status=1 and date not null and want to insert ID and Date in the same format in a text file.
The text file should have
1000 2014-02-18 20:32:20.657
2000 2014-02-18 20:32:20.658
Here's the code.
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString))
{
connection.Open();
SqlCommand cmdSel = new SqlCommand(sqlSelNew, connection);
SqlDataReader reader1 = cmdSel.ExecuteReader();
while (reader1.Read())
{
DataSet ds = GetData(sqlSelNew);
CultureInfo _provider = CultureInfo.InvariantCulture;
ID = Convert.ToInt32(reader1["ID"].ToString());
string dtformat = #"yyyy/MM/dd HH:mm:ss.fff";
var d = DateTime.ParseExact(dateresized,dtformat , _provider);
using (StreamWriter sw = new StreamWriter(txtfilepath, true))
{
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(ID + " " + d);
sw.Flush();
sw.Close();
}
I get "String was not recognized as a valid DateTime." error. How can I handle this?
Thanks
Rashmi

First, you shouldn't have to parse the date. You should simply be able to use reader.GetDateTime() to read that column and assign it. Second, why are you both filling up a DataSet and using a SqlDataReader to get the values directly? I'd expect one or the other but not both. Third, you ought to be wrapping your reader in a using statement as it implements IDisposable.
using (var reader1 = cmdSel.ExecuteReader())
{
while (reader1.Read())
{
var id = reader1.GetInt32(0);
var date = reader1.GetDateTime(1);
...
}
}

Related

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version in C#

I am having a problem with my C# code, I put the query in C# code it returns the error as below:
Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':= (0 + 1) as numA, ID, UID, File, StartDate, EndDate FROM OEE_PROD.thermalValue' at line 1.
I run the query in MySql database it work and can get the data.
This is my code:
public void exportCSVBtn_Click(object sender, EventArgs e)
{
string conn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(conn))
{
string a = (string)Session["Userid"];
using (MySqlCommand cmd = new MySqlCommand("select A.*, timediff(B.StartDate, A.EndDate) from (select #rownumA:= (#rownumA + 1) as numA, ID, UID, File, StartDate, EndDate FROM OEE_PROD.thermalValue where File = '" + ddrKdf.SelectedItem.Text + "'AND UserID='" + a + "' order by ID) A LEFT JOIN(select #rownumB:= (#rownumB + 1) as numB, ID as BID, StartDate, EndDate FROM OEE_PROD.thermalValue where File = '" + ddrKdf.SelectedItem.Text + "'AND UserID='" + a + "'order by ID) B ON B.numB = (A.numA + 1)"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
cmd.Parameters.AddWithValue("#rownumB", 0);
cmd.Parameters.AddWithValue("#rownumA", 0);
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Build the CSV file data as a Comma separated string.
string csv = string.Empty;
foreach (DataColumn column in dt.Columns)
{
//Add the Header row for CSV file.
csv += column.ColumnName + ',';
}
//Add new line.
csv += "\r\n";
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
//Add the Data rows.
csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
}
//Add new line.
csv += "\r\n";
}
//Download the CSV file.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=KDFExport_" + DateTime.Now + ".csv");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(csv);
Response.Flush();
Response.End();
}
}
}
}
}
The error message is caused by you treating #rownumA and #rownumB mysql user defined variables as a C# query parameter and provide 0 as its value with the following lines:
cmd.Parameters.AddWithValue("#rownumB", 0);
cmd.Parameters.AddWithValue("#rownumA", 0);
This means that #rownumA:= (#rownumA + 1) mysql expression becomes 0:= (0 + 1), which is obviously not correct.
If you want to use sql user defined variables, then add the following parameter to the connection string of your .Net connector connection:
Allow User Variables=True
or
AllowUserVariables=True
This option was added to connector v5.2.2
This way you can remove the parameter assignment lines from your C# code and the mysql variables do not get substituted.
However, the file and userid field values in the where clause indeed should be supplied via parameters and not through string concatenation!
Let's structure this an alternative way: run the two queries separately, download their data in order, then write the CSV using both results. This in contrast to getting mysql to join the data on a fake number created by row order.
using (MySqlDataAdapter daA = new MySqlDataAdapter ("
SELECT ID, UID, File, StartDate, EndDate
FROM OEE_PROD.thermalValue
WHERE File = #file
ORDER BY ID", connstr
))
using (MySqlDataAdapter daB = new MySqlDataAdapter ("
SELECT StartDate
FROM OEE_PROD.thermalValue
WHERE File = #file AND UserID = #userID
ORDER BY ID", connstr
))
{
DataTable dtA = new DataTable();
DataTable dtB = new DataTable();
daA.SelectCommand.Parameters.AddWithValue("#file", ddrKdf.SelectedItem.Text);
daB.SelectCommand.Parameters.AddWithValue("#file", ddrKdf.SelectedItem.Text);
daB.SelectCommand.Parameters.AddWithValue("#userID", a);
daA.Fill(dtA);
daB.Fill(dtB);
for(int i = 0; i < dtA.Rows.Count; i++)
{
string csvTimeDiffCol = "";
if(i+1 < dtB.Rows.Count){
DateTime st = (DateTime)dtA.Rows[i]["StartDate"];
DateTime ed = (DateTime)dtB.Rows[i+1]["EndDate"];
//now you can choose how do you want to represent this timespan?
csvTimeDiffCol = (ed - st).ToString();
}
//now write the row to CSV, including the csvTimeDiffCol string
}
}
Notes:
I've effectively rewritten your code here from what I could guess at your algorithm from what I saw in your code: "get db to prepare a superset and a subset of records, ordered by their ID, join them together on row position with an off-by-one offset" - I personally think this is wonky but I can't fault the logic because it's not mine
We download the two result sets then step over them in order, taking relevant rows
I've no idea what your timediff or dates looks like; I've assumed that they're Dates and you're looking for the difference in hours minutes etc between them, hence I cast them to DateTime in the c# and then used x-y to turn them into a TimeSpan; you'll probably need to format this
I haven't written your csv code in, because that part is unchanged. There are libraries to help with that though; you don't need to roll your own
This algorithm may not be perfect/may need debugging. I don't present it as a "paste this and I did your work for you" - I'm presenting it as something to get you to think about the problem another way

Read out Double and DateTime from NOSQL Database

I want to read out a double and date from a SQLite Database with C#.
Database:
date: numeric
money: real
Code:
SQLiteConnection dbConnection = new SQLiteConnection("Data Source = " + nameDB + ".sqlite; Version = 3;");
dbConnection.Open();
String sql = $"SELECT * FROM banking";
SQLiteCommand command = new SQLiteCommand(sql, dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("date: " + Convert.ToDateTime( reader["date"]) );
Console.WriteLine("money: " + reader["money"]);
}
dbConnection.Close();
The problem is in my database data is this:
date: 28.06.2017 14:45:24
money: 20,50
And my output was this:
reader[date]: 28,06
reader[money]: 20
Ho can i say the reader to read the money as double and date as DateTime ?
reader.GetDouble(reader.GetOrdinal("money"));
reader.GetDateTime(reader.GetOrdinal("date"))
refer to Docs for all the available methods for SqliteDataReader
Use Parse methods for double and DateTime. Try like:
while (reader.Read())
{
var date =DateTime.Parse(reader["date"].ToString());
var money = double.Parse(reader["money"].ToString("0.##"));
}

The conversion of the varchar value '5566778891' overflowed an int column

I am using SQL Server database and there's a column named Cell(VARCHAR) data type. While reading using reader.Read() I get the Conversion error. Can anyone kindly explain the reason for the error?
This is my code:
string myConnection = dbController.connectionString;
string query1 = "SELECT ID, Name from Manager Where Cell = " + managerNo.Text;
using (var conn = new SqlConnection(myConnection))
using (var cmd = new SqlCommand(query1, conn))
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
managerID = reader.GetString(0);
mgrID.Text = managerID;
managerNames.Text = reader.GetString(1);
}
conn.Close();
}
I am reading the value from a textbox (managerNo). I have tested the query on SQL Server Management Studio as well:
select Name, DOB
from Contact
where Cell = 1233453411
When I use Cell = 1233453411 without the value as string I get the conversion error, however using Cell = '1233453411' as a string the result is fetched.
Whats the reason for this and how to solve this issue.
Thanks.
This is a comparison between two different types, a string and an integer:
where Cell = 1233453411
SQL Server has to decide which type to use. It decides on the more restrictive type, which is the number. So, the string gets converted to a number.
Say, you have a cell phone in New York with a number like: 917-555-5555. Well, that becomes a number like 9,175,555,555 and this exceeds the value of the maximum integer. Hence, you would get a conversion overflow.
The moral: Always use similar types when making comparisons.
EDIT:
What should you do? Don't store telephone numbers as numbers; they should be stored as strings (for instance, leading zeroes can be important).
If could do a quick-and-dirty and put single quotes around the value of the parameter. But, what you should really do is change your SQL code to use a parameter with a string type. It is bad programming to just stuff values (particularly user input) into a query string.
Your code is working fine in SQL Server 2008 R2. (Below is tested code)
private void button1_Click(object sender, EventArgs e)
{
string str = "Server=.\\SQL2008R2;Database=Practice;User ID=sa;Password=123;Trusted_Connection=True;Connection Timeout=0";
string query1 = "SELECT * from tblTest Where Cell = " + textBox1.Text;
using (var conn = new SqlConnection(str))
using (var cmd = new SqlCommand(query1, conn))
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string aa = reader.GetString(0);
}
conn.Close();
}
Otherwise using can change you query like in below format.
string query1 = "SELECT * from tblTest Where Cell = '" + textBox1.Text + "' ";
Thanks

DateTime format possible to implement on reader.read?

I am very new to c# and visual studios 2015 so any opinions are happily accepted(even bad ones).
Is there a way to directly pull out DateTime to a format of "ddd" using reader.read like for example:
textBox1.Text = Convert.ToDateTime(reader[1].ToString()).ToShortDateString();
I have a very basic grasp of DateTime to string but not sure if the it can be placed in reader.read or not.
My data table contents is this:
And my textBox1 shows this:
Trying to make that text box show "Thu" instead of the date from my code below:
if (btnSearch.Text == "Search")
{
string sqlStmt = #"SELECT * FROM dbo.tbl_employees WHERE emp_id = #emp_id;";
using (SqlConnection dbCon = new SqlConnection(conStr))
{
SqlCommand dbCmd = new SqlCommand(sqlStmt, dbCon);
dbCmd.Parameters.AddWithValue("#emp_id", txtSearchID.Text);
dbCon.Open();
SqlDataReader reader = dbCmd.ExecuteReader();
while (reader.Read())
{
textBox1.Text = Convert.ToDateTime(reader[1].ToString()).ToShortDateString();
}
reader.Close();
dbCon.Close();
}
}
Sql:
[day_received] Date NOT NULL,
Thanks.
Assuming that your reader object is of type MySqlDataReader, a modification of the following should work:
using(var reader = cmd.ExecuteReader()){
if(reader.Read())
textBox1.Text = reader.GetDateTime(fieldIndex)
.ToShortDateString();
}
using(var reader = cmd.ExecuteReader())
{
if(reader.Read())
{
/// Considering GetDateTime returns a DateTime object...
DateTime dtTemp = reader.GetDateTime(fieldIndex);
textBox1.Text = dtTemp.toString("dddd");
}
}
Is this what you are looking for?

Correct DateTime format in SQL Server CE?

I have a C# DateTime class and wanted to know how I need to format it in a SQL Server CE query to insert it into the database, I was hoping to have both the date and time inserted in. Currently when I try variations thereof I get invalid format exceptions.
Current format I'm using is: dd/MM/yyyy, was hoping to do something like dd/MM/yyyy hh:mm:ss.
The way I'm trying to do the insert is like so:
( ( DateTime )_Value ).ToString( "dd/MM/yyyy hh:mm:ss" )
Obviously hh:mm:ss isn't working, if that isn't there dd/MM/yyyy executes successfully in the query.
I've tried a few formats including what I've found on google but none have worked so far...
If you're worried about getting the format right at all, something has already gone seriously wrong. There are two things you need to do to correctly work with datetime values in any database, not just sqlce:
Make sure you're using a datetime type for the column (not a text type like varchar)
Make sure you're using a datetime parameter in a parameterized query, and not string concatenation.
If you do that, there is no formatting involved on your part. At all. Example:
void SetDate(int recordID, DateTime timeStamp)
{
string SQL = "UPDATE [sometable] SET someDateTimeColumn= #NewTime WHERE ID= #ID";
using (var cn = new SqlCeConnection("connection string here"))
using (var cmd = new SqlCeCommand(SQL, cn))
{
cmd.Parameters.Add("#NewTime", SqlDbType.DateTime).Value = timeStamp;
cmd.Parameters.Add("#ID", SqlDbType.Integer).Value = recordID;
cn.Open();
cmd.ExecuteNonQuery();
}
}
Never ever ever ever EVER use string manipulation to substitute values into sql queries. It's a huge no-no.
Try the following format:
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")
Man, you do not need to convert string to DateTime.
Use a instance of a new DateTime and pass the date as parameter. Like this:
using (var ctx = new DBPreparoEntities())
{
var _client = from p in ctx.Client
select new Client
{
data = new DateTime(2016,08,17),
dateconf = null,
scod_cli = p.Rua,
valorini = 7214.62m,
};
return client.ToList();
}
don't use:
... data = DateTime.Parse("2016/12/10") // or other type convertions.
private void button1_Click(object sender, EventArgs e)
{
var cnn1 ="";//connection string
SqlCeConnection cnn = new SqlCeConnection(cnn1);
datetime dt4 = DateTime.Today.Date.ToString("yyyyMMdd").trim();//format
var qry ="insert into tbl_test(User_Id, DateOfJoin)values (11,'" + dt4 + "')";
cmd = new SqlCeCommand(qry, cnn);
try
{
dr = cmd.ExecuteReader();
}
catch (Exception ex)
{
string sr = ex.Message;
throw;
}
}
Above code worked for me.
sorry this is in vb.net, but this is a method i use to convert from a CE date/time format:
Public Shared Function ConvertSqlDateTimeFormat(ByVal s As String) As String
Dim theDate As New Text.StringBuilder
Dim sTemp As String = ""
Dim iIndex As Integer
If s.Length > 8 Then
'first we do the time
iIndex = s.IndexOf(" ", System.StringComparison.OrdinalIgnoreCase)
If iIndex > 0 Then
sTemp = s.Substring(iIndex).Trim
iIndex = sTemp.IndexOf(".", System.StringComparison.OrdinalIgnoreCase)
If iIndex > 0 Then
sTemp = sTemp.Substring(0, iIndex)
End If
End If
'next the date
theDate.Append(s.Substring(4, 2))
theDate.Append("/")
theDate.Append(s.Substring(6, 2))
theDate.Append("/")
theDate.Append(s.Substring(0, 4))
theDate.Append(" ")
theDate.Append(sTemp)
End If
Return theDate.ToString
End Function

Categories

Resources