SoFunction
Updated on 2025-04-08

Learn from scratch - Basics

Learn how to use placeholders in SQL

I'm so busy with the whale these days and haven't written it for several days. I'm so sorry for everyone. XHTML has been unhappy these days. Although I knew this before, I didn't care much. But now I'm awakening from my dream, and I find that XHTML is a signal, so I have to study this carefully these days. Very stupid, I haven't discovered the mystery of XHTML. That's true. I haven't been on the table before, so I really don't know how delicious this table of dishes is. Let's talk less, let's get back to the topic.

First rewrite the CommandText of the previous Command:
= "INSERT INTO NewsArticle (Title, SubTitle, Writer, Content)"
+ " valueS ( @Title, @SubTitle, @Writer, @Content )";
Is the code clear? We can avoid a large number of difficult-to-understand statements.

Next, we assign attributes and values ​​to these locators:
("@Title", , 50);
("@SubTitle", , 50);
("@Writer", , 10);
("@Content", );

The above is defined as a value type for each placeholder, which is not difficult to understand. The receiver gives the value again:
["@Title"].value = ();
if ( == || () == "")
{
["@SubTitle"].value = ;
}
else
{
["@SubTitle"].value = ();
}
if ( == || () == "")
{
["@Writer"].value = ;
}
else
{
["@Writer"].value = ;
}
["@Content"].value = ;

Actually, you can also write this way:
("@Title", , 50) .value = ();
It depends on what you like.

Not bad, is the program very organized?

Program code:
private void btnPublish_ServerClick(object sender, e)
{
string strConnection = ["SqlDatabase1"];

// Connect to the SqlServer database
SqlConnection conn = new SqlConnection(strConnection);
// Create SqlCommand
SqlCommand cmd = ();
= "INSERT INTO NewsArticle (Title, SubTitle, Writer, Content)"
+ " valueS ( @Title, @SubTitle, @Writer, @Content )";
();

("@Title", , 50);
("@SubTitle", , 50);
("@Writer", , 10);
("@Content", );

["@Title"].value = ();
if ( == || () == "")
{
["@SubTitle"].value = ;
}
else
{
["@SubTitle"].value = ();
}
if ( == || () == "")
{
["@Writer"].value = ;
}
else
{
["@Writer"].value = ;
}
["@Content"].value = ;
// Insert record
try
{
();
}
catch (Exception ex)
{
("<script language=javascript>alert("Input error, the database cannot be updated.")</script>");
}
finally
{
();
}
}