C语言中怎么把运行得到的数据保存到下次运行?

C语言中怎么把运行得到的数据保存到下次运行?,第1张

存入数据文件

每次程序开始时从数据库或文件中读取

每次程序结束前将数据写入到数据库或文件中

举个例子

从键盘输入两个学生数据,写入一个文件中, 再读出这两个学生的数据显示在屏幕上。

#include<stdio.h>

struct stu

{

char name[10]

int num

int age

char addr[15]

}boya[2],boyb[2],*pp,*qq

main()

{

FILE *fp

char ch

int i

pp=boya

qq=boyb

if((fp=fopen("stu_list","wb+"))==NULL)

{

printf("Cannot open file strike any key exit!")

getch()

exit(1)

}

printf("ninput datan")

for(i=0i<2i++,pp++)

scanf("%s%d%d%s",pp->name,&pp->num,&pp->age,pp->addr)

pp=boya

fwrite(pp,sizeof(struct stu),2,fp)

rewind(fp)

fread(qq,sizeof(struct stu),2,fp)

printf("nnnametnumber age addrn")

for(i=0i<2i++,qq++)

printf("%st%5d%7d%sn",qq->name,qq->num,qq->age,qq->addr)

fclose(fp)

}

获取datagridview单元格修改后的值,然后用这个值去更新数据库就可以了,下面上代码

try

            {

                SqlConnection scon = new SqlConnection("数据库连接字")

                scon.Open()

                SqlCommand scmd

                for (int i = 0 i < dataGridView1.Rows.Count i++)

                {

                    string id = dataGridView1.Rows[i].Cells["id"].EditedFormattedValue.ToString()

                    string name = dataGridView1.Rows[i].Cells["name"].EditedFormattedValue.ToString()

                    string age = dataGridView1.Rows[i].Cells["age"].EditedFormattedValue.ToString()

                    string address = dataGridView1.Rows[i].Cells["address"].EditedFormattedValue.ToString()

                    string scmdStr = "update studentInfo set name='" + name + "',age='" + age + "',address='" + address + "' where id ='" + id + "'"

                    scmd = new SqlCommand(scmdStr, scon)

                    scmd.ExecuteNonQuery()

                }

                scon.Close()

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.ToString())

            }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6424691.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-22
下一篇 2023-03-22

发表评论

登录后才能评论

评论列表(0条)

保存