可以在C#中使用SQL的AND语句引用多个C#变量吗?如果可以,如何做?

huangapple go评论71阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月6日 22:52:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定