sqlserver string operation
时间: 2023-08-21 12:04:07 浏览: 158
SQL Server 中有很多字符串操作函数,以下是一些常用的函数:
1. LEFT(string, length):返回字符串 string 最左边的 length 个字符。
2. RIGHT(string, length):返回字符串 string 最右边的 length 个字符。
3. SUBSTRING(string, start, length):返回字符串 string 从 start 开始的 length 个字符。
4. LEN(string):返回字符串 string 的长度。
5. REPLACE(string, old_string, new_string):将字符串 string 中的 old_string 替换为 new_string。
6. UPPER(string):将字符串 string 转换为大写。
7. LOWER(string):将字符串 string 转换为小写。
8. LTRIM(string):去除字符串 string 左边的空格。
9. RTRIM(string):去除字符串 string 右边的空格。
10. CHARINDEX(substring, string):返回子字符串 substring 在字符串 string 中的位置。
这些函数可以用于 SQL Server 中的 SELECT 语句,如:
SELECT LEFT('Hello World', 5)
返回结果为 "Hello"。
相关问题
C# sql server删除表中所有数据
在C#中,如果你想从SQL Server数据库的表中删除所有数据,可以使用ADO.NET提供的SqlCommand对象执行SQL DELETE ALL语句。这里是一个基本示例:
```csharp
using System.Data.SqlClient;
string connectionString = "Data Source=your_server_name;Initial Catalog=your_database_name;Integrated Security=True"; // 替换为你的连接字符串
// 创建SqlCommand实例
SqlCommand command = new SqlCommand("DELETE FROM your_table_name", new SqlConnection(connectionString));
try
{
// 打开数据库连接
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
Console.WriteLine("Deleting all data from the table...");
// 执行删除操作
int rowsAffected = command.ExecuteNonQuery(); // 返回受影响的行数
if (rowsAffected > 0)
Console.WriteLine($"{rowsAffected} rows deleted successfully.");
else
Console.WriteLine("No rows to delete or operation failed.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred: {ex.Message}");
}
学生选课管理系统javaweb,sqlserver代码实现
以下是一个简单的学生选课管理系统的JavaWeb代码,使用SQL Server作为数据库。本代码实现了学生登录、选课、查看选课情况等功能。请注意该代码仅供参考。
1.数据库设计
学生表(Student)
- ID(学生ID)
- Name(学生姓名)
- Password(学生密码)
课程表(Course)
- ID(课程ID)
- Name(课程名称)
- Teacher(任课教师)
- Credit(学分)
选课表(Selection)
- ID(选课ID)
- StudentID(学生ID)
- CourseID(课程ID)
2.登录页面(index.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生选课管理系统</title>
</head>
<body>
<h1>学生选课管理系统</h1>
<form action="login" method="post">
<label for="id">学号:</label>
<input type="text" id="id" name="id"><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
3.登录处理(LoginServlet.java)
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String password = request.getParameter("password");
String message = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=StudentCourse";
String user = "sa";
String pass = "123456";
Connection con = DriverManager.getConnection(url, user, pass);
String sql = "SELECT * FROM Student WHERE ID=? AND Password=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
HttpSession session = request.getSession(true);
session.setAttribute("id", id);
response.sendRedirect("course");
return;
} else {
message = "学号或密码错误";
}
rs.close();
pstmt.close();
con.close();
} catch (ClassNotFoundException e) {
message = "数据库驱动程序未找到";
e.printStackTrace();
} catch (SQLException e) {
message = "数据库操作异常";
e.printStackTrace();
}
request.setAttribute("message", message);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
4.选课页面(course.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生选课管理系统</title>
</head>
<body>
<h1>学生选课管理系统</h1>
<h2>欢迎,<%=session.getAttribute("id")%></h2>
<h3>当前选课情况:</h3>
<table>
<tr>
<th>课程编号</th>
<th>课程名称</th>
<th>任课教师</th>
<th>学分</th>
<th>操作</th>
</tr>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%
String id = (String)session.getAttribute("id");
String message = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=StudentCourse";
String user = "sa";
String pass = "123456";
Connection con = DriverManager.getConnection(url, user, pass);
String sql1 = "SELECT * FROM Selection WHERE StudentID=?";
PreparedStatement pstmt1 = con.prepareStatement(sql1);
pstmt1.setString(1, id);
ResultSet rs1 = pstmt1.executeQuery();
List<String> courses = new ArrayList<String>();
while (rs1.next()) {
courses.add(rs1.getString("CourseID"));
}
rs1.close();
pstmt1.close();
String sql2 = "SELECT * FROM Course";
PreparedStatement pstmt2 = con.prepareStatement(sql2);
ResultSet rs2 = pstmt2.executeQuery();
while (rs2.next()) {
String courseID = rs2.getString("ID");
String courseName = rs2.getString("Name");
String teacher = rs2.getString("Teacher");
String credit = rs2.getString("Credit");
String operation = "";
if (courses.contains(courseID)) {
operation = "<a href='drop?id=" + courseID + "'>退选</a>";
} else {
operation = "<a href='choose?id=" + courseID + "'>选课</a>";
}
out.print("<tr><td>" + courseID + "</td><td>" + courseName + "</td><td>" + teacher + "</td><td>" + credit + "</td><td>" + operation + "</td></tr>");
}
rs2.close();
pstmt2.close();
con.close();
} catch (ClassNotFoundException e) {
message = "数据库驱动程序未找到";
e.printStackTrace();
} catch (SQLException e) {
message = "数据库操作异常";
e.printStackTrace();
}
%>
</table>
<p><%=message%></p>
</body>
</html>
5.选课处理(ChooseServlet.java)
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/choose")
public class ChooseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
String id = (String)session.getAttribute("id");
String courseID = request.getParameter("id");
String message = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=StudentCourse";
String user = "sa";
String pass = "123456";
Connection con = DriverManager.getConnection(url, user, pass);
String sql = "INSERT INTO Selection(StudentID, CourseID) VALUES(?, ?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, courseID);
pstmt.executeUpdate();
pstmt.close();
con.close();
} catch (ClassNotFoundException e) {
message = "数据库驱动程序未找到";
e.printStackTrace();
} catch (SQLException e) {
message = "数据库操作异常";
e.printStackTrace();
}
request.setAttribute("message", message);
request.getRequestDispatcher("course.jsp").forward(request, response);
}
}
6.退选处理(DropServlet.java)
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/drop")
public class DropServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
String id = (String)session.getAttribute("id");
String courseID = request.getParameter("id");
String message = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=StudentCourse";
String user = "sa";
String pass = "123456";
Connection con = DriverManager.getConnection(url, user, pass);
String sql = "DELETE FROM Selection WHERE StudentID=? AND CourseID=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, courseID);
pstmt.executeUpdate();
pstmt.close();
con.close();
} catch (ClassNotFoundException e) {
message = "数据库驱动程序未找到";
e.printStackTrace();
} catch (SQLException e) {
message = "数据库操作异常";
e.printStackTrace();
}
request.setAttribute("message", message);
request.getRequestDispatcher("course.jsp").forward(request, response);
}
}
注意:本代码仅供参考,请不要直接用于实际应用中,需要根据实际情况进行修改和完善。
阅读全文