如何在使用SQL查询时更改文件名? [英] How to change file name in use SQL query?

查看:86
本文介绍了如何在使用SQL查询时更改文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第1类



 public void TNameFunction()
{
string filenumber =从表中选择ID。
string filename =TName+ filenumber;

Directory.CreateDirectory(path + filename); -------> 此文件名错误。(文件名:表中的TNameSelect ID)但我想(文件名:Tname + 2或3,4(Tname + filenumber)
//来做tnamefunction txt代码
}





从这里class2转到tname函数。



第2类:创建函数类



 static int Indexvalue =(从表中选择ID)

public void TName(){
foreach(tablecontrol中的字符串表)
{
if(table1)
name = table1;
else if( table2)
name = table2;
else
name =T+ Indexvalue; -------------> 我想索引值就是这样。


等。

}
}







如何解决这个问题。



我尝试过:



如何解决这个文件名号问题?



这个写代码结果。

文件名:TNameSelect ID来自表格。



B我想要TName3或TName4 ..等。

解决方案

 string filenumber =从表中选择ID。

这个永远不会工作。

以下是CodeProject中的两篇初学者文章,用于与C#连接到SQL数据库。

初学者指南,通过C#访问SQL Server [ ^ ]

如何将SQL数据库连接到您的C#程序,初学者的教程 [ ^ ]



作为数字的ID应存储在 int long 不是字符串所以该行应该类似

  long  filenumber = GetIDFromTable() ; 

你可以通过研究我上面给你的文章来编写函数 GetIDFromTable



一旦你有文件号创建一个基于它的文件名是微不足道的(参见 String.Format方法(系统)| Microsoft Docs [ ^ ])

  string  filename =  string  .Format(  T {0},filenumber); 



您的代码

 Directory.CreateDirectory(路径+文件名); 

也不起作用。 CreateDirectory执行它所说的...它创建一个目录(文件夹)而不是文件。所以不要将文件名传递给它。



您还需要阅读有关创建文件或文件夹的文档 - 如何:创建文件或文件夹(C#编程指南) | Microsoft Docs [ ^ ]使用Path.Combine获取完整路径文件名,例如

  string  fullFileName = System.IO.Path.Combine(path,fileName); 



这应该让你坚持一段时间。您可能需要回复一个关于代码的新问题

  if (table1)
name = table1;
else if (table2)
name = table2;


Class 1

public void TNameFunction()
{
     string filenumber=Select ID  from table.
     string filename= "TName"+filenumber;

      Directory.CreateDirectory(path+filename);  -------> This file name error.(filename:TNameSelect ID from table) but I want to (filename:Tname+2 or 3,4 (Tname+filenumber)
       //to do tnamefunction txt code  
 }



from here class2 goes to tname function.

Class 2 : Create function class

static int Indexvalue=(Select ID from table)

public void TName(){
foreach(string table in tablecontrol)
{
if(table1)
name=table1;
else if(table2)
name=table2;
else
name="T" + Indexvalue; -------------> I want to index value is what it comes.
.
.
etc.

}
}




How to solve this problem.

What I have tried:

How to solve this file name number problem?

This write code result.
Filename: TNameSelect ID From table.

But I want to TName3 or TName4 ..etc.

解决方案

string filenumber=Select ID  from table.

This is never going to work.
Here are two beginners articles in CodeProject for connection to a SQL database with C#
Beginners guide to accessing SQL Server through C#[^]
How to connect SQL Database to your C# program, beginner's tutorial[^]

ID being a number should be stored in an int or a long not a string so that line should be something like

long filenumber = GetIDFromTable();

where you will write the function GetIDFromTable by studying the articles I gave you above.

Once you have the file number creating a filename based on it is trivial (see String.Format Method (System) | Microsoft Docs[^])

string filename = string.Format("T{0}", filenumber);


Your code

Directory.CreateDirectory(path+filename);

also won't work. CreateDirectory does what it says ... it creates a directory (folder) not a file. So don't pass a file name into it.

You also need to read the documentation on creating a file or folder - How to: Create a File or Folder (C# Programming Guide) | Microsoft Docs[^]Use Path.Combine to get the fully pathed filename e.g.

string fullFileName = System.IO.Path.Combine(path, fileName);


That should keep you going for a while. You might need to come back with a new question about the code

if(table1)
name=table1;
else if(table2)
name=table2;

but at the moment it's not clear what you are trying to do


这篇关于如何在使用SQL查询时更改文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆