function n() { var x = ""; try { x = window.json_ua.toString() } catch (y) { x = "" } return x }中的window.json_ua.toString() 数值是怎么出现的
时间: 2024-02-14 09:28:54 浏览: 44
根据提供的代码,`window.json_ua.toString()`表示将`window.json_ua`对象转换为字符串。`window.json_ua`是一个全局变量,它的值可能是在代码的其他地方被设置的。具体来说,您需要查看在代码中是否有其他地方对`window.json_ua`进行了赋值操作,以确定它的值是如何出现的。
相关问题
public void Reload() { var dbName = _configurationRoot.GetSection("SqliteConnection").Value; var dirPath = Path.GetDirectoryName(dbName.Substring(dbName.IndexOf("=")+1)); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } dbName = dbName.Substring(0, dbName.LastIndexOf(@"\") == -1 ? 0 : dbName.LastIndexOf(@"\") + 1); dbName = $"{dbName}MigrateDb_{DateTime.Now.ToString("yyyyMMddHHmmss")}.db"; MigrateDbContext.DbName = dbName; string json = File.ReadAllText("appsettings.json"); var setting = JsonSerializer.Deserialize<SettingProperties>(json); setting.SqliteConnection = dbName; var serialize = JsonSerializer.Serialize(setting); using var stream = new FileStream("appsettings.json", FileMode.Create, FileAccess.ReadWrite); using StreamWriter streamWriter = new StreamWriter(stream); streamWriter.WriteLine(serialize); _context?.Dispose(); _context = new MigrateDbContext(); _context.Database.EnsureCreatedAsync(); } 优化这段代码
首先,我们可以把一些重复的代码提取出来,比如获取数据库名和创建目录的部分,可以写成一个单独的方法,代码如下:
```csharp
private string GetDbNameAndCreateDirectory(string connectionString)
{
var dbName = connectionString.Substring(connectionString.IndexOf("=") + 1);
var dirPath = Path.GetDirectoryName(dbName);
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
dbName = dbName.Substring(0, dbName.LastIndexOf(@"\") == -1 ? 0 : dbName.LastIndexOf(@"\") + 1);
dbName = $"{dbName}MigrateDb_{DateTime.Now:yyyyMMddHHmmss}.db";
return dbName;
}
```
然后,我们可以将获取连接字符串和更新配置文件的部分也提取出来,写成一个方法:
```csharp
private void UpdateConnectionString(string newConnectionString)
{
string json = File.ReadAllText("appsettings.json");
var setting = JsonSerializer.Deserialize<SettingProperties>(json);
setting.SqliteConnection = newConnectionString;
var serialize = JsonSerializer.Serialize(setting);
using var stream = new FileStream("appsettings.json", FileMode.Create, FileAccess.ReadWrite);
using StreamWriter streamWriter = new StreamWriter(stream);
streamWriter.WriteLine(serialize);
}
```
最后,我们可以将 Reload 方法中的代码进行重构,将每个步骤都提取出来,使代码更加清晰易懂,代码如下:
```csharp
public void Reload()
{
var connectionString = _configurationRoot.GetSection("SqliteConnection").Value;
var newConnectionString = GetDbNameAndCreateDirectory(connectionString);
UpdateConnectionString(newConnectionString);
_context?.Dispose();
_context = new MigrateDbContext();
_context.Database.EnsureCreatedAsync();
}
```
这样,我们就将代码进行了优化,使其更加清晰易懂、易于维护。
dataInfo = new DeviceManage.设备采购(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Selected == true) { if (isadmin == "1") { if (row.Cells[0].Value == null) { dataInfo.buy_no = cgdh.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); dataInfo.login_no = yhbh.Text = dataGridView1.Rows[i].Cells[3].Value.ToString(); dataInfo.dev_name = xbmc.Text = dataGridView1.Rows[i].Cells[4].Value.ToString(); dataInfo.dev_model = xbxh.Text = dataGridView1.Rows[i].Cells[5].Value.ToString(); dataInfo.gys_name = gys.Text = dataGridView1.Rows[i].Cells[6].Value.ToString(); dataInfo.buy_date = dateTimePicker1.Text = dataGridView1.Rows[i].Cells[7].Value.ToString(); dataInfo.price = dj.Text = dataGridView1.Rows[i].Cells[8].Value.ToString(); dataInfo.num = sl.Text = dataGridView1.Rows[i].Cells[9].Value.ToString(); }
将上述代码中的 `for` 循环改为 `foreach` 循环,并将代码中的索引变量 `i` 替换为 `row.Index`,可以使用以下代码:
```csharp
dataInfo = new DeviceManage.设备采购();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Selected == true)
{
if (isadmin == "1")
{
if (row.Cells[0].Value == null)
{
dataInfo.buy_no = cgdh.Text = row.Cells[2].Value.ToString();
dataInfo.login_no = yhbh.Text = row.Cells[3].Value.ToString();
dataInfo.dev_name = xbmc.Text = row.Cells[4].Value.ToString();
dataInfo.dev_model = xbxh.Text = row.Cells[5].Value.ToString();
dataInfo.gys_name = gys.Text = row.Cells[6].Value.ToString();
dataInfo.buy_date = dateTimePicker1.Text = row.Cells[7].Value.ToString();
dataInfo.price = dj.Text = row.Cells[8].Value.ToString();
dataInfo.num = sl.Text = row.Cells[9].Value.ToString();
}
}
}
}
```
在 `foreach` 循环中,`row` 直接表示集合中的每个元素,可以直接使用 `row.Cells[0]` 访问该行中的第一列单元格。使用 `row.Index` 可以获取当前行的索引。注意,`foreach` 循环中的变量是只读的,不能修改它们的值,因此需要使用 `row.Index` 来代替原来的索引变量 `i`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)