英文:
Can one use an SQL AND-statement in C# referencing multiple C# variables? If so, how?
问题
I'm trying to get the following code to work properly in C# so that the selection from my database will be dependent on two columns (ID and StartTime) rather than just one.
我正在尝试使以下代码在C#中正常工作,以便从我的数据库中选择依赖于两个列(ID和StartTime),而不仅仅是一个。
myUpdateData.UpdateData(dtUpdate, "SELECT EntryRecord, StartTime, EndTime, ID FROM tblTimeManagement WHERE ID = " + cmbxEmployees.SelectedIndex + " AND StartTime = " + DateTime.Today);
This same structure has worked in another capacity without the AND-statement in another part of my program where ID is the only focus of the WHERE in the SQL statement. Am I going about this wrong, or is this not possible in C#?
在我的程序的另一部分中,没有AND语句,这个相同的结构已经在另一个容量中正常工作,在那里ID是SQL语句中WHERE的唯一重点。我是不是走错了方向,或者在C#中不可能实现这个?
The functioning code I mentioned above is as follows:
我上面提到的可运行代码如下:
myUpdateData.UpdateData(dtUpdate, "SELECT ID, FamilyName, GivenName, Address, Phone, PayRate FROM tblEmployees WHERE ID = " + int.Parse(txtFamilyName.Tag.ToString()));
英文:
I'm trying to get the following code to work properly in C# so that the selection from my database will be dependent on two columns (ID and StartTime) rather than just one.
myUpdateData.UpdateData(dtUpdate, "SELECT EntryRecord, StartTime, EndTime, ID FROM tblTimeManagement WHERE ID = " + cmbxEmployees.SelectedIndex " AND StartTime = " + DateTime.Today);
This same structure has worked in another capacity without the AND-statement in another part of my program where ID is the only focus of the WHERE in the SQL statement. Am I going about this wrong, or is this not possible in C#?
The functioning code I mentioned above is as follows:
myUpdateData.UpdateData(dtUpdate, "SELECT ID, FamilyName, GivenName, Address, Phone, PayRate FROM tblEmployees WHERE ID = " + int.Parse(txtFamilyName.Tag.ToString()));
答案1
得分: 1
I think one way that you should familiarize yourself with, as a new developer, is string.Format
.
DISCLAIMER - It is true that you should use commands and parameters when you are updating an SQL table, BUT when all of the code is internal and risk is low ---
I have used string.Format
to bring various data into a SQL statement (select, insert, update, delete)
Here is a sample using your query.
var query = string.Format(@"SELECT
EntryRecord, StartTime, EndTime, ID
FROM
tblTimeManagement
WHERE
ID = '{0}' AND StartTime = '{1:yyyy-MM-dd}'",
cmbxEmployees.SelectedIndex, DateTime.Now);
The static function string.Format
has multiple overloads, but in this case we are using this one:
string Format(string format, params object[] args)
It takes a template for your string and then any array of objects. Within the format
parameter, your string will contain a set of tokens that are placeholders for your various values (SelectedIndex, Current Time, etc). They are represented as integers wrapped with braces, i.e.: ({0}
, {1}
). You must begin from zero, then onward sequentially based on how many variables you have.
You can also format the variables as they are parsed into your query. I have demonstrated this with the datetime format in the second variable placeholder ({1:yyyy-MM-dd}
).
更多信息: https://learn.microsoft.com/en-us/dotnet/api/system.string.format
英文:
I think one way that you should familiarize yourself with, as a new developer, is string.Format
.
DISCLAIMER - It is true that you should use commands and parameters when you are update a SQL table, BUT when all of the code is internal and risk is low ---
I have use string.Format
to bring various data into a SQL statement (select, insert, update, delete)
Here is a sample using your query.
var query = string.Format(@"SELECT
EntryRecord, StartTime, EndTime, ID
FROM
tblTimeManagement
WHERE
ID = '{0}' AND StartTime = '{1:yyyy-MM-dd}'",
cmbxEmployees.SelectedIndex, DateTime.Now);
The static function string.Format
has multiple overloads, but in this case we are using this one:
string Format(string format, params object[] args)
It takes a template for your string and then any array of objects. Within the format
parameter, your string will contain a set of tokens that are placeholder for your various values (SelectedIndex, Current Time, etc). They are represented as integers wrapped with braces, i.e.: ({0}
, {1}
). You must begin from zero, then onward sequentially based on how many variables you have.
You can also format the variables as they are parsed into your query. I have demonstrated this with the datetime format in the second variable placeholder ({1:yyyy-MM-dd}
).
more information: https://learn.microsoft.com/en-us/dotnet/api/system.string.format
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论