1数据库建表,
create table mytable(
pkid nvarchar(40) not null,
a nvarchar(40) null
)
insert into mytable(pkid,a) values(newid(),'测试')
2新建对应此表的实体类
[Serializable]
public class mytable
{
private String _pkid = "";
public String pkid
{
get { return _pkid; }
set { _pkid = value; }
}
private String _a = "";
public String a
{
get { return _a; }
set { _a = value; }
}
}
3新建 *** 作类:
public class mytableDAO
{
private String _ConnectionString;
public mytableDAO(String ConnectionString)
{
this_ConnectionString = ConnectionString;
}
public int New(EntitiesMsgRecord obj)
{
String sql = "insert into mytable(pkid,a) values(@pkid,@a)";
SqlConnection cn = new SqlConnection(this_ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmdParametersAddWithValue("@a", obja);
cmdParametersAddWithValue("@pkid",
StringEmptyEquals(objpkid) SystemGuidNewGuid()ToString() : objpkid);
try
{
if (cnState != ConnectionStateOpen)
cnOpen();
return cmdExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cnState != ConnectionStateClosed)
cnClose();
}
}
public int Update(Entitiesmytable obj)
{
String sql = "Update mytable Set a=@a Where pkid=@ObjectID";
SqlConnection cn = new SqlConnection(this_ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmdParametersAddWithValue("@a", obja);
cmdParametersAddWithValue("@pkid", objpkid);
try
{
if (cnState != ConnectionStateOpen)
cnOpen();
return cmdExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cnState != ConnectionStateClosed)
cnClose();
}
}
public int Del(Entitiesmytable obj)
{
String sql = "delete from mytable Where pkid=@ObjectID";
SqlConnection cn = new SqlConnection(this_ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmdParametersAddWithValue("@ObjectID", objpkid);
try
{
if (cnState != ConnectionStateOpen)
cnOpen();
return cmdExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cnState != ConnectionStateClosed)
cnClose();
}
}
public int Del(String where)
{
String sql = StringFormat("delete from mytable Where {0}", whereToLower()Replace("update", "")Replace("delete", ""));
SqlConnection cn = new SqlConnection(this_ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
try
{
if (cnState != ConnectionStateOpen)
cnOpen();
return cmdExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cnState != ConnectionStateClosed)
cnClose();
}
}
public DataTable Query(String where)
{
String sql = StringFormat("select from mytable Where {0}", whereToLower()Replace("update", "")Replace("delete", "")Replace("insert", "")Replace(";", "")Replace("--", "")Replace("exec", ""));
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(this_ConnectionString));
DataTable dt = new DataTable();
daFill(dt);
return dt;
}
catch
{
return null;
}
}
}
4在ui中调用:
41 新增:
private void NewData()
{
String conn=SystemConfigurationConfigurationManagerConnectionStrings["dbCon"]ConnectionString
mytableDao dao=new mytableDao(conn);
mytable obj=new mytable();
obja=thistxtbox_aText;
objb=thistxtbox_bText;
objc=thistxtbox_cText;
daoNew(obj);
}
42 取数据绑定GridView
private void LoadData_InitGridView()
{
String conn=SystemConfigurationConfigurationManagerConnectionStrings["dbCon"]ConnectionString
mytableDao dao=new mytableDao(conn);
Table dt=daoQuery(StringFormat(" a ='{0}'",thistxtbox_aText));
//gridview
thisGridView1DataSource=dt;
thisGridView1DataBind();
//textbox control
thistextbox_aText=dtRows[0][0]ToString();
}
43删除
private void Del()
{
String conn=SystemConfigurationConfigurationManagerConnectionStrings["dbCon"]ConnectionString
mytableDao dao=new mytableDao(conn);
//根据条件删除指定记录
daoDel(where);
}
44更新
private void Update()
{
String conn=SystemConfigurationConfigurationManagerConnectionStrings["dbCon"]ConnectionString
mytableDao dao=new mytableDao(conn);
daoUpdate(obj);//obj 为mytable的一个实例:需要更新的
}
5配置文件中连接信息
<connectionStrings>
<add name="dbCon" connectionString="Data Source=1921681100;Initial Catalog=Northwind;User ID=sa;PassWord=sa" providerName="SystemDataSqlClient"/>
</connectionStrings>
6读取连接信息
SystemConfigurationConfigurationManagerConnectionStrings["dbCon"]ConnectionString
概述
单纯只用多个数组管理学生成绩信息,不使用结构体,该程序最主要的难点是依据学号或总成绩对学生信息进行排序,借助了临时数组来标记排好序的下标。
运行结果如下:
输入数据:
根据总成绩排序:
根据学号删除学生信息:
贴上代码(有点多)
#include <stdioh>
#include <stdlibh> //exit函数头文件
#include <stringh> //字符串相关 *** 作头文件
#define MAX_STUDENT 30 //最大学生数
//函数声明,本程序共10个子函数,每个函数对应一个 *** 作
void student_scanf(int n);
void student_printf(int n);
int student_find_name(int n);
int student_find_num(int n);
void student_sort_num(int n);
void student_sort_sum(int n);
int student_alter_num(int n);
int student_alter_name(int n);
int student_delete_num(int n);
int student_delete_name(int n);
//全局数组变量,用于存储学生信息
char names[MAX_STUDENT][50];
int math[MAX_STUDENT];
int english[MAX_STUDENT];
int computer[MAX_STUDENT];
int sum[MAX_STUDENT];
int num[MAX_STUDENT];
//以下变量用于学生信息数组排序,作为临时数组
int temp_num[MAX_STUDENT];
char temp_names[MAX_STUDENT][50];
int temp_math[MAX_STUDENT];
int temp_english[MAX_STUDENT];
int temp_computer[MAX_STUDENT];
int temp_sum[MAX_STUDENT];
//sort数组存储排好序的学号或姓名下标
int sort[MAX_STUDENT];
//循环全局变量
int i, j;
//main主函数
int main(void)
{
int choice,n;
while (1)
{
printf("\n");
printf("欢迎使用学生成绩管理系统\n");
printf("[1] 输入所有学生信息\n");
printf("[2] 输出所有学生成绩\n");
printf("[3] 按学号查找某个学生信息\n");
printf("[4] 按姓名查找某个学生信息\n");
printf("[5] 按学号对学生排序\n");
printf("[6] 按总成绩对学生排序\n");
printf("[7] 按学号修改某个学生信息\n");
printf("[8] 按姓名修改某个学生信息\n");
printf("[9] 按学号删除某个学生信息\n");
printf("[10] 按姓名删除某个学生信息\n");
printf("[0] 退出程序\n");
printf("请输入您的选择(0 - 9):");
scanf("%d",&choice);
printf(")\n");
switch (choice)
{
case 1://录入;
printf("请输入录入的学生信息数: ");
scanf("%d",&n);
student_scanf(n);
break;
case 2://输出;
student_printf(n);
break;
case 3://根据学号查找
student_find_num(n);
break;
case 4://根据姓名查找
student_find_name(n);
break;
case 5://按学号排序
student_sort_num(n);
break;
case 6://按姓名排序
student_sort_sum(n);
break;
case 7://按学号修改
student_alter_num(n);
break;
case 8://按姓名修改
student_alter_name(n);
break;
case 9://按学号删除
student_delete_num(n);
n--;
break;
case 10://按姓名删除
student_delete_name(n);
n--;
break;
case 0://退出程序
printf("退出程序\n");
printf("程序结束,谢谢使用!\n");
exit(0);
default:
printf("您输入的菜单有误。请重新输入!\n");
}
}
return 0;
}
//1输入信息
void student_scanf(int n)
{
for (i = 0; i<n; ++i)
{
printf("\n请输入第%d个学生的信息:\n", i + 1);
printf("\n学号:");
scanf("%d", &num[i]);
printf("\n姓名:");
scanf("%s", names[i]);
printf("\n数学成绩:");
scanf("%d", &math[i]);
printf("\n英语成绩:");
scanf("%d", &english[i]);
printf("\n计算机成绩:");
scanf("%d", &computer[i]);
//计算总成绩
sum[i] = math[i] + english[i] + computer[i];
}
}
//2打印信息
void student_printf(int n)
{
printf("\n学号\t姓名\t数学成绩\t英语成绩\t计算机成绩\t总成绩\n");
printf("----------------------------------------------------------\n");
for (i = 0; i<n; ++i)
{
printf("%d\t%s\t%d\t\t%d\t\t%d\t\t%d\n", num[i], names[i], math[i], english[i], computer[i], sum[i]);
}
printf("-------------------------------------------------------\n");
}
//3按学号查找
int student_find_num(int n)
{
int nums;
int result;
printf("请输入待查找的学生学号:");
scanf("%d",&nums);
result= -1;
for (i = 0; i<n; ++i)
{
if (nums == num[i])
{
result = i;
break;
}
}
//最后判断q值
if (result == -1)
{
printf("没有该学生信息!\n");
return 0;
}
else
{
//先打印表头
printf("\n学号\t\t姓名\t\t数学成绩\t英语成绩\t计算机成绩\t总成绩\n");
//再打印数据
printf("%d\t\t%s\t\t%d\t\t%d\t\t%d\t%d\n", num[result], names[result], math[result], english[result], computer[result], sum[result]);
printf("打印出查找结果!\n");
}
return 1;
}
//4用姓名查找成绩
int student_find_name(int n)
{
char name[200];
int result;
printf("请输入待查找的学生姓名:");
scanf("%s", name);
result = -1;
for (i = 0; i<n; ++i)
{
if (strcmp(name, names[i]) == 0)
{
result = i;
break;
}
}
if (result == -1)
{//未找到结果
printf("没有该学生信息!\n");
return 0;
}
else//找到结果
{
printf("\n学号\t姓名\t数学成绩\t英语成绩\t计算机成绩\t总成绩\n");
printf("%d\t%s\t%d\t\t%d\t\t%d\t\t%d\n", num[result], names[result], math[result], english[result], computer[result], sum[result]);
printf("已完成查找!\n");
}
return 1;
}
//5按学号排序
void student_sort_num(int n)
{
int min,max;
for(i=0; i<n; ++i) //复制临时数组
{
temp_num[i] = num[i];
}
max = 0; //查找学号最大值,将其下标存至sort数组的最后一个值中
for(j=1; j<n; j++)
{
if(temp_num[max]<temp_num[j])
max = j;
}
sort[n-1] = max; //sort数组的最后一个数
for(i=0; i<n-1; ++i)
{
min = i; //查找学号最小值
for(j=0; j<n; ++j)
{
if(temp_num[min]>temp_num[j])
min = j;
}
//sort数组记录排序的学生信息的下标
sort[i] = min;
temp_num[min] = temp_num[max]; //利用临时数组将查找过的学生信息的学号设为最大值,排除查找干扰
}
for(i=0; i<n; ++i) //再复制一次临时数组
{
temp_num[i] = num[i];
strcpy(temp_names[i],names[i]);
temp_math[i] = math[i];
temp_english[i] = english[i];
temp_computer[i] = computer[i];
temp_sum[i] = sum[i];
}
for(i=0; i<n; i++) //按照下标对原数组进行修改
{
num[i] = temp_num[sort[i]];
strcpy(names[i],temp_names[sort[i]]);
math[i] = temp_math[sort[i]];
english[i] = temp_english[sort[i]];
computer[i] = temp_computer[sort[i]];
sum[i] = temp_sum[sort[i]];
}
printf("排序完毕,请按菜单键2查看排序结果!\n");
return ;
}
//6按总成绩排序
void student_sort_sum(int n)
{
int min,max;
for(i=0; i<n; ++i) //复制临时数组
{
temp_sum[i] = sum[i];
}
max = 0; //查找总成绩最大值,将其下标存至sort数组的最后一个值中
for(j=1; j<n; j++)
{
if(temp_sum[max]<temp_sum[j])
max = j;
}
sort[n-1] = max; //sort数组的最后一个数
for(i=0; i<n-1; ++i)
{
min = i; //查找总成绩最小值
for(j=0; j<n; ++j)
{
if(temp_sum[min]>temp_sum[j])
min = j;
}
//sort数组记录排序的学生信息的下标
sort[i] = min;
temp_sum[min] = temp_sum[max]; //利用临时数组将查找过的学生信息的总成绩设为最大值,排除查找干扰
}
for(i=0; i<n; ++i) //再复制一次临时数组
{
temp_num[i] = num[i];
strcpy(temp_names[i],names[i]);
temp_math[i] = math[i];
temp_english[i] = english[i];
temp_computer[i] = computer[i];
temp_sum[i] = sum[i];
}
for(i=0; i<n; i++) //按照下标对原数组进行修改
{
num[i] = temp_num[sort[i]];
strcpy(names[i],temp_names[sort[i]]);
math[i] = temp_math[sort[i]];
english[i] = temp_english[sort[i]];
computer[i] = temp_computer[sort[i]];
sum[i] = temp_sum[sort[i]];
}
printf("排序完毕,请按菜单键2查看排序结果!\n");
return ;
}
//7按学号修改学生信息
int student_alter_num(int n)
{
int nums;
int result;
printf("请输入待修改的学生学号:");
scanf("%d",&nums);
result= -1;
for (i = 0; i<n; ++i)
{
if (nums == num[i])
{
result = i;
break;
}
}
//最后判断q值
if (result == -1)
{
printf("没有该学生信息!\n");
return 0;
}
else //修改信息值
{
printf("请重新输入该学生信息:\n");
printf("学号:\n");
scanf("%d",&num[result]);
printf("姓名:\n");
scanf("%s",names[result]);
printf("数学成绩:\n");
scanf("%d",&math[result]);
printf("英语成绩:\n");
scanf("%d",&english[result]);
printf("计算机成绩:\n");
scanf("%d",&computer[result]);
sum[result] = math[result] + english[result] + computer[result];
}
return 1;
}
//8按姓名修改学生信息
int student_alter_name(int n)
{
char name[50];
int result;
printf("请输入待修改的学生姓名:");
scanf("%s",name);
result= -1;
for (i = 0; i<n; ++i)
{
if (strcmp(name,names[i])==0)
{
result = i;
break;
}
}
//最后判断q值
if (result == -1)
{
printf("没有该学生信息!\n");
return 0;
}
else //修改信息值
{
printf("请重新输入该学生信息:\n");
printf("学号:\n");
scanf("%d",&num[result]);
printf("姓名:\n");
scanf("%s",names[result]);
printf("数学成绩:\n");
scanf("%d",&math[result]);
printf("英语成绩:\n");
scanf("%d",&english[result]);
printf("计算机成绩:\n");
scanf("%d",&computer[result]);
sum[result] = math[result] + english[result] + computer[result];
}
return 1;
}
//9按学号删除学生信息
int student_delete_num(int n)
{
int nums;
int result;
printf("请输入待删除的学生学号:");
scanf("%d",&nums);
result= -1;
for (i = 0; i<n; ++i)
{
if (nums == num[i])
{
result = i;
break;
}
}
//最后判断q值
if (result == -1)
{
printf("没有该学生信息!\n");
return 0;
}
else //删除当前学生信息即为将数组从result的位置依次前挪一个位置
{
for(i=result; i<n-1; ++i) //最后在main函数中,要将n的值减1
{
num[i] = num[i+1];
strcpy(names[i],names[i+1]);
math[i] = math[i+1];
english[i] = english[i+1];
computer[i] = computer[i+1];
sum[i] = sum[i+1];
}
}
return 1;
}
//10按姓名删除学生信息
int student_delete_name(int n)
{
char name[50];
int result;
printf("请输入待删除的学生姓名:");
scanf("%s",name);
result= -1;
for (i = 0; i<n; ++i)
{
if (strcmp(name,names[i])==0)
{
result = i;
break;
}
}
//最后判断q值
if (result == -1)
{
printf("没有该学生信息!\n");
return 0;
}
else //删除当前学生信息即为将数组从result的位置依次前挪一个位置
{
for(i=result; i<n-1; ++i) //最后在main函数中,要将n的值减1
{
num[i] = num[i+1];
strcpy(names[i],names[i+1]);
math[i] = math[i+1];
english[i] = english[i+1];
computer[i] = computer[i+1];
sum[i] = sum[i+1];
}
}
return 1;
}
import javaawtevent;
import javaxswing;
import javaawt;
import javasqlConnection;
import javasqlDriverManager;
import javasqlResultSet;
import javasqlSQLException;
import javasqlStatement;
public class DeleteFrame extends JFrame implements ActionListener {
private MainFrame parentF;
JTextField jt1=new JTextField();
JTextField jt2=new JTextField();
JTextField JTextField_free=new JTextField();
JButton jb=new JButton("删除");
JOptionPane JOptionPane1 = new JOptionPane();
public DeleteFrame(MainFrame m){
super("删除学生信息 ");
parentF=m;
thissetSize( 400,250);
thissetDefaultCloseOperation(JFrameDISPOSE_ON_CLOSE);
Container c=thisgetContentPane();
csetLayout(null);
JLabel j1=new JLabel("学 号");
JLabel j2=new JLabel("姓 名");
cadd(j1);
j1setBounds(10,10,60,30);
cadd(jt1);
jt1setBounds(70,10,100,30);
cadd(j2);
j2setBounds(200,10,60,30);
cadd(jt2);
jt2setBounds(250,10,100,30);
cadd(jb);
jbsetBounds(100,180,60,40);
jbaddActionListener(this);
thisaddWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
parentFsetVisible(true);
shutDown();
}
});
}
public void shutDown(){
thisdispose();
}
public void deleteRecord(int id){
try{
ClassforName("sunjdbcodbcJdbcOdbcDriver");
}
catch(ClassNotFoundException ex){
Systemexit(-1);
}
try{
String URL="jdbc:odbc:student1";
Connection con =DriverManagergetConnection(URL);
Statement stmt1=concreateStatement();
String SQL="delete from 成绩表 where id = " + id;
ResultSet rs=stmt1executeQuery(SQL);
Systemoutprintln(stmt1);
stmt1close();
conclose();
JTextField_freesetText("");
JOptionPane1showMessageDialog(this,"删除成功","消息",
JOptionPaneINFORMATION_MESSAGE);
}
catch(SQLException ex){
exprintStackTrace();
}
}
public void actionPerformed(ActionEvent e){
if(egetSource()==jb){
thisdeleteRecord(id);//id为所删记录的主键值
}
}
}
简单写了一个,给你参考参考
#!/bin/bashfunction fun-mycheck
{
dialog --textbox mydatatxt 20 80
}
function fun-myappend
{
dialog --inputbox "添加姓名" 20 40 2>>mydatatxt
echo "" >>mydatatxt
}
function fun-mydelete
{
dialog --inputbox "输入姓名" 20 40 2>mytemptxt
temp=`cat mytemptxt`
cat mydatatxt|sed '/'$temp'/d'>mytemptxt
cat mytemptxt>mydatatxt
}
function fun-alter
{
dialog --inputbox "输入要修改的姓名" 20 40 2>mytemptxt
echo "" >>mytemptxt
dialog --inputbox "更改为" 20 40 2>>mytemptxt
temp=`cat mytemptxt|head -n 1`
temp2=`cat mytemptxt|tail -n 1`
cat mydatatxt|sed 's/'$temp'/'$temp2'/'>mytemptxt
cat mytemptxt>mydatatxt
}
function fun-total
{
echo "共有"`wc -l mydatatxt|gawk '{print $1}'`"个学生">mytemptxt
dialog --textbox mytemptxt 10 20
}
touch mytemptxt
touch mydatatxt
while [ 1 ]
do
dialog --menu "小超人幼儿园小五班" 20 80 10 1 "查看所有信息" 2 "添加学生" 3 "删除学生" 4 "修改学生资料" 5 "数据统计" 6 "退出" 2>mytemptxt
if [ $ -eq 1 ]
then
break 1
fi
case `cat mytemptxt` in
1)
fun-mycheck ;;
2)
fun-myappend ;;
3)
fun-mydelete ;;
4)
fun-alter ;;
5)
fun-total ;;
6)
break 1 ;;
esac
done
以上就是关于C#编程实现学生信息的增加和根据学号删除指定学生的信息全部的内容,包括:C#编程实现学生信息的增加和根据学号删除指定学生的信息、用C语言编写一个学生管理系统。、学生管理系统的删除学生信息 删除代码请修改一下。。急需帮助。。马上就要交作业了等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)