
当然也可以使用存储过程
INSERT INTO 表名 VALUES (,,,,) 有几个值就插入几个
就是这样呀
建立连接
写连接字符串
然后SqlCommand
然后执行
DBhelper里面的
public static string connString = "Data Source=;Initial Catalog=PatientsDB;User Id=sa";
/// <summary>
/// 执行查询,并返回数据库受影响的行数
/// </summary>
/// <param name="sql">要执行的 SQL 语句</param>
/// <returns>返回数据库受影响的行数</returns>
public static int ExecuteNonQuery(string sql)
{
SqlConnection connection = null;
try
{
connection = new SqlConnection(connString);
SqlCommand command = new SqlCommand(sql, connection);
connectionOpen();
return commandExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (connection != null)
connectionClose();
}
}
/// <summary>
/// 执行查询并返回数据库受影响的行数,该方法通常用来执行 INSERT、UPDATE、DELETE 查询
/// </summary>
/// <param name="commandText">要执行查询的 SQL 语句</param>
/// <returns>数据库受影响的行数</returns>
public static int ExecuteNonQuery(string commandText)
{
SqlConnection connection = null;
try
{
connection = new SqlConnection(connString);
SqlCommand command = new SqlCommand(commandText, connection);
connectionOpen();
return commandExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (connection != null)
connectionClose();
}
}使用PHP和MySQL实现。
HTML代码:php code
<!DOCTYPE html>
<html>
<head>
<title>网站对接数据库示例</title>
</head>
<body>
<h1>网站对接数据库示例</h1>
<form method="post" action="submitphp">
<label for="input">输入框:</label>
<input type="text" name="input" id="input" required>
<input type="submit" value="提交">
</form>
<form method="post" action="queryphp">
<label for="query">查询:</label>
<input type="text" name="query" id="query" required>
<input type="submit" value="查询">
</form>
</body>
</html>
submitphp代码:php code
<php
// 连接数据库
$db_host = "localhost"; // 数据库服务器地址
$db_user = "root"; // 数据库用户名
$db_pwd = ""; // 数据库密码
$db_name = "test"; // 数据库名
$conn = mysqli_connect($db_host, $db_user, $db_pwd, $db_name);
// 获取输入框内容
$input = $_POST['input'];
// 将输入框内容插入数据库
$sql = "INSERT INTO input (content) VALUES ('$input')";
mysqli_query($conn, $sql);
// 关闭数据库连接
mysqli_close($conn);
echo "提交成功!";
>
queryphp代码:php code
<php
// 连接数据库
$db_host = "localhost"; // 数据库服务器地址
$db_user = "root"; // 数据库用户名
$db_pwd = ""; // 数据库密码
$db_name = "test"; // 数据库名
$conn = mysqli_connect($db_host, $db_user, $db_pwd, $db_name);
// 获取查询框内容
$query = $_POST['query'];
// 查询数据库
$sql = "SELECT FROM input WHERE content LIKE '%$query%'";
$result = mysqli_query($conn, $sql);
// 输出查询结果
while ($row = mysqli_fetch_assoc($result)) {
echo $row['content'] "<br>";
}
// 关闭数据库连接
mysqli_close($conn);
>
以上代码实现了一个简单的网站对接数据库的功能,用户可以在输入框中输入文字并提交,程序将输入框内容保存到数据库中。用户也可以在查询框中输入关键字并提交,程序将查询数据中匹配的内容并输出。通过Ajax等技术将数据提交给后端服务器。在前端页面中,用户通过表单组件输入的值可以通过JavaScript获取,然后通过Ajax等技术将数据提交给后端服务器。后端服务器可以使用不同的编程语言和框架来处理接收到的数据,并将这些数据存储到数据库中。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)